How to remove comma(,) from second child

Advertisements

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>

Expected output:

One, Two

>Solution :

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>

Leave a ReplyCancel reply