0

So basically, I am copying content of one div to other like this

$("#button").click(function() {
    var a = $("#div1").html()
    $("#div2").html(a);

I want the variable 'a' to change all the occurrence of a character/string and copy it to div2.

$("#button").click(function() {
   var a = $("#div1").html().replace("$money","$cash");
   $("#div2").html(a);

So, basically, I want the replace the occurrence of '$money' with '$cash' and store it inside test1. The above code copies the exact content but does not replace '$money' with '$cash'.

What am I doing wrong?

Dhiraj
  • 2,687
  • 2
  • 10
  • 34

2 Answers2

0

Escape back to PHP to substitute variables into the Javascript.

$("#button").click(function() {
   var a = $("#div1").html().replace(/<?php echo preg_quote($money); ?>/g, <?php echo json_encode($cash); ?>);
   $("#div2").html(a);
});

You need to use a regexp with the global modifier to perform multiple replacements.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

repeating response as @Barmar, but removing the php part to simplify

$("#button").click(function() {
   var a = $("#div1").html().replace(/money/g,"cash");
   $("#div2").html(a);  
 });
dhiman
  • 385
  • 3
  • 9