Divyesh K
Divyesh K

Reputation: 109

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.

Upvotes: 4

Views: 126

Answers (1)

Tushar
Tushar

Reputation: 87203

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>

Upvotes: 7

Related Questions