0

This is my code in which I am trying to replace Dollar Symbol $ with INR(Indian Rupee) using jQuery, however it is not working as this is only a specimen code so it is not ul >li and find text and replace text kinda thing, my tries goes like this- I tried to first gather whole HTML and then replace $ sign with the code, but its not making anyhow.

My assumption - in $(document).ready() using $(this) inside this snippet refers to the document, let me know if this is a false assumption, as I tried my attempts keeping this thing in mind.

Please DO NOT give any links/referrals to do it in CSS way or Webrupee API usage, keep it simple to jQuery this time :)

My Code -

<!DOCTYPE html>
<html>
<head>
    <title>Rupee Change - jQuery Flavour</title>
</head>

<body>
<p>This is a price listing with &#36; sign, Now I wanted to replace each dollar sign with rupee symbol</p>
<ul>
<li>&#36; 500</li>
<li>&#36; 600</li>
<li>&#36; 700</li>
<li>&#36; 800</li>
<li>&#36; 900</li>
</ul>

 <div id="showhtml"></div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(this).html().replace("$", "<del>&#2352;</del>");
});
</script>
</body>
</html> 

FYI - I also tried this way of handling but not working :( find-text-string-using-jquery

Community
  • 1
  • 1
swapnesh
  • 26,318
  • 22
  • 94
  • 126

1 Answers1

4

$ is a special character in Regex. You will need to escape it.

$(document).ready(function(){
    var replaced = $('body').html().replace(/\$/g, "<del>&#2352;</del>");
    $('body').html(replaced);
});
Rok Burgar
  • 949
  • 5
  • 7
  • This helped me, but for some reason I had to use two slashes to get it to work with a dollar sign, like so: replace(/\\$/g, ""); – kent_e Apr 10 '17 at 20:53