inaz inaz - 2 months ago 5
CSS Question

Remove all commas from a string in Jquery

I have many classes and want to remove the commas in them if any commas exist there.

I wrote the below code but the code does not work correctly. The second class value is replaced with the first value.




var removebox = $(".remove"),
removeboxHtml = removebox.html();
removebox.html(removeboxHtml.replace(/,/g , ''));

<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
</head>
<body>
<span class="remove">,17500000</span>
<span class="remove">,2479000</span>
</body>
</html>




Answer

Try this. Updated your code:

   

$(".remove").each(function(){
   
    $(this).html($(this).html().replace(/,/g , ''));
});
   
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
</head>
<body>
<span class="remove">,17500000</span>
<span class="remove">,2479000</span>
</body>
</html>

Comments