Change all instances of a string inside of a td

How would I alter the following line to change all instances of the string “OldName” to “NewName” inside of a targeted td? Right now it only changes the first instance and then stops.


    tdText.html(tdText.html().replace(OldName, NewName));

For reference purposes below is the code that line came from


       var OldName = $('[id^=ImageName]').val(),
            NewName = $('[id^=NewName]').val(),
            tdText = $("." + DataKey).parent();
            NewName = data["itemName"];
       subOld = OldName.split('.');
       subNew = NewName.split('.');
       NewName = subNew[0] + "." + subOld[1];
       tdText.html(tdText.html().replace(OldName, NewName));

Hi there,

Use a RegEx object, with a global switch.
This should do it for you:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Replace example</title>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
  </head>

  <body>
    <table width="960" border="0">
      <tr>
        <td id="replace">"blank.png" some text "blank.png" some more text "blank.png"</td>
        <td>&nbsp;</td>
      </tr>
    </table>

    <script>
      $(document).ready(function() {
        OldName = "blank.png";
        NewName = "checker.jpg";
        tdText = $("#replace");
        tdText.html(tdText.html().replace(new RegExp(OldName, 'g'), NewName));
      });
    </script>
  </body>
</html>

Sweet … Thanks Again Pullo…