Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace multiple $ sign using jquery

I am not able to replace multiple $ signs using JavaScript/jQuery , my JavaScript replace code are as per bellow,

var str = $('#amt').html().replace("/\$/g","₹");
alert(str);

but it does not replace all occurrence, Please help me to replace $ by symbol.

like image 785
Divyesh K Avatar asked Mar 13 '23 22:03

Divyesh K


1 Answers

Your regex is correct, but when wrapped it in quotes, it is no longer a RegEx, it's a string.

.replace(/\$/g, "₹");

And the HTML is not replaced it is just creating a string variable, use

$('#amt').html(function (i, oldHtml) {
    return oldHtml.replace(/\$/g, "₹");
});

$('#amt').html(function(i, oldHtml) {
  return oldHtml.replace(/\$/g, "₹");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="amt">
  <div>Books: $150.00</div>
  <div>Food: $2050.00</div>
  <div>Total: $2200.00</div>
</div>
like image 167
Tushar Avatar answered Mar 22 '23 23:03

Tushar