Replace multiple $ icons using jquery

I can't replace multiple characters $using JavaScript / jQuery, my JavaScript replacement code is below,

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

but it does not replace all occurrences. Please help me replace the character $with .

+4
source share
1 answer

Your regular expression is correct, but when it is wrapped in quotation marks, it is no longer case-sensitive, but a string .

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

And HTML is not replaced, it just creates 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>
Run codeHide result
+7
source

Source: https://habr.com/ru/post/1619472/


All Articles