www.webdeveloper.com
Results 1 to 3 of 3

Thread: Problem with text replace script

  1. #1
    Join Date
    Jun 2007
    Posts
    3

    Problem with text replace script

    I can't get the script to work that will replace old text with new text in specified th:

    html content:
    Code:
    <table align="center" cellspacing="1" class="homepagemodule report" id="trade_bait">
    	<caption>
    		<span>Trade Bait</span>
    	</caption>
    	<tbody>
    	<tr>
    		<th class="franchisename">Franchise</th>
    		<th class="willgiveup">Will Give Up</th>
    		<th class="inexchangefor">In Exchange For</th>
    		<th>Trade?</th>
    	</tr>
    I would like to replace "Will Give Up" in second th with "Offered" and here's script I did that didn't worked:

    Code:
    function replaceText () {
    	var offeredparent = document.getElementById('trade_bait').getElementsByTagName("tbody")[0].getElementsByTagName('tr').getElementsByTagName('th')[1];
    	var str = offeredparent.textNode.nodeValue;
    	tr = str.replace('Will Give Up','Offered');
    	offeredparent.textNode.nodeValue = str;
    }
    Could use help to correct this or suggest different script that would work better.

    appreciate the help!

    johnny
    Last edited by johnny41; 07-08-2007 at 05:09 PM.

  2. #2
    Join Date
    Aug 2004
    Posts
    244
    you need to replace this:

    PHP Code:
    tr str.replace('Will Give Up','Offered'); 
    with:

    PHP Code:
    tr str.replace(/Will Give Up/g,'Offered'); 
    The 'g' means its global and doesnt just replace the first instance

  3. #3
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    getElementsByTagName('tr') needs a [0]... I'd personally write this as:
    Code:
    document.getElementById('trade_bait').rows[0].cells[1].firstChild.data="Offered"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles