Home > Mobile >  How to remove comma(,) from second child
How to remove comma(,) from second child

Time:11-18

I want to remove comma(,) from second child. In console.log() remove comma perfectly but in tag comma is not removed

Here down is my code:

$(document).ready(function() {
   var child = $("span").children()[1];
   $(child).html().replace(/,/g , ''); // in second child comma is not removed
   console.log($(child).html().replace(/,/g , '')); // remove comma perfectly
});
li { 
    display: inline-block; 
}
<!DOCTYPE html>
<html>
<head>
<title>Try jQuery Online</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<span>
    <li>One, </li>
    <li>Two,</li>
</span>
</body>
</html>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Expected output:

One, Two

CodePudding user response:

You must set the value to the DOM Element

$(child).html($(child).html().replace(/,/g , ''));

$(document).ready(function() {
   var child = $("span").children()[1];
   $(child).html($(child).html().replace(/,/g , '')); // You must set the value to the DOM
   console.log($(child).html().replace(/,/g , '')); // remove comma perfectly
});
li { 
    display: inline-block; 
}
<!DOCTYPE html>
<html>
<head>
<title>Try jQuery Online</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<span>
    <li>One, </li>
    <li>Two,</li>
</span>
</body>
</html>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related