Replace text

In this code:


<td>
      "blank.png"
      <br>
      <a class="sample" href="javascript:void(0);" onclick="some function">Rename</a>
</td>

I am trying to change “blank.png” to “checker.jpg” without altering anything else in the td field. Below is what I have tried. I can load the contents of the TD tag into the tdText variable, but I can’t change the value I need to change. Any Ideas where I am going wrong?


OldName = "blank.png"
NewName = "checker.jpg"
$(".imageRename").click(function () {
     tdText = $(sample).parent().html();
     tdText.replace(OldName, NewName);
     $(sample).parent().html(tdText);
        
});


Hi there,

You could do it like this:

<!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"<br><a class="sample" href="javascript:void(0);" onclick="some function">Rename</a></td>
        <td>&nbsp;</td>
      </tr>
    </table>

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

In this case I’ve given the table cell an id, but there are a number of ways to get a reference to it, depending on your mark-up.

Perfect … Thanks Pullo