Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with replace text 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!

    DoubleJ
    Last edited by DoubleJ; 07-08-2007 at 10:01 PM.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Try:
    Code:
    function replaceText () {
            var offeredparent = document.getElementById('trade_bait').getElementsByTagName("tbody")[0].getElementsByTagName('tr')[0].getElementsByTagName('th')[1];
            var newtext = document.createTextNode("Offered");
            var oReplaceNode = offeredparent.childNodes(0);
            oReplaceNode.replaceNode(newtext);
    }

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    function replaceText () {
    	var offeredparent = document.getElementById('trade_bait').getElementsByTagName("tbody")[0].getElementsByTagName('tr')[0].getElementsByTagName('th')[1];
    	offeredparent.childNodes[0].data=offeredparent.childNodes[0].data.replace('Will Give Up','Offered');
    }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Tried both "suggested" scripts and they didn't worked. Here's demo page with one of two "suggested" scripts: http://deafiron.com/2007/test/demoreplace.html

    DoubleJ

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Both scripts should work, but I do not see where you call the function on the page posted

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Need to trigger this function when the page is loaded so I added "window.onload" to do it:

    Code:
    window.onload = function replaceText () {
    	var offeredparent = document.getElementById('trade_bait').getElementsByTagName("tbody")[0].getElementsByTagName('tr')[0].getElementsByTagName('th')[1];
    
    	offeredparent.childNodes[0].data=offeredparent.childNodes[0].data.replace('Will Give Up','Offered');
    }
    It works: http://deafiron.com/2007/test/demoreplace.html

    but it seems that onload disrupt the tabs (trade_bait table resides in "On the Block" section) to work (see at the bottom page): http://deafiron.com/2007/test/test.html

    Any alternative method instead of onload?

    DoubleJ

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    On /test/test.html 'offeredparent' is undefined as the script runs before the table 'trade_bait' is rendered.
    Put the script below the table.

  • #8
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    test.html is updated but still no cigar...

    DoubleJ

  • #9
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    If you use the window.onload = function, the script could be in the head.
    If you put the script below the table, just the bare script is need without the onload.

    It should work if the 'trade_bait' is where it is expected in the DOM.

  • #10
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did tried without onload and still doesn't work, I prolly missed something: http://deafiron.com/2007/test/demoreplace.html

    I found an advanced onload function to use instead of basic onload that seem have addressed the tabs issue:

    Code:
    addLoadListener(init);
    
    function init() {
    
    	var offeredparent = document.getElementById('trade_bait').getElementsByTagName("tbody")[0].getElementsByTagName('tr')[0].getElementsByTagName('th')[1];
    	offeredparent.childNodes[0].data=offeredparent.childNodes[0].data.replace('Will Give Up','Offered');
    	
    	return true;
    }
    
    function addLoadListener(fn) {
    	if (typeof window.addEventListener != 'undefined') {
    		window.addEventListener('load', fn, false);
    	}
    	else if (typeof document.addEventListener != 'undefined') {
    		document.addEventListener('load', fn, false);
    	}
    	else if (typeof window.attachEvent != 'undefined') {
    		window.attachEvent('onload', fn);
    	}
    	else {
    		var oldfn = window.onload;
    		if (typeof window.onload != 'function') {
    			window.onload = fn;
    		}
    		else {
    			window.onload = function() {
    				oldfn();
    				fn();
    			};
    		}
    	}
    }
    and here's link to updated test.html: http://deafiron.com/2007/test/test.html

    and much appreciate your help with replace script!

    DoubleJ


  •  

    Posting Permissions

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