SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
        tdText.html(tdText.html().replace(OldName, NewName));
    For reference purposes below is the code that line came from

    Code:
           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));

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

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

    HTML Code:
    <!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>

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet ... Thanks Again Pullo...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •