...

View Full Version : Problem with replace text script



DoubleJ
07-08-2007, 09:56 PM
I can't get the script to work that will replace old text with new text in specified th:

html content:


<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:



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

rwedge
07-08-2007, 10:52 PM
Try:
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);
}

vwphillips
07-08-2007, 11:01 PM
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');
}

DoubleJ
07-08-2007, 11:53 PM
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

rwedge
07-09-2007, 12:36 AM
Both scripts should work, but I do not see where you call the function on the page posted

DoubleJ
07-09-2007, 01:57 AM
Need to trigger this function when the page is loaded so I added "window.onload" to do it:



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

rwedge
07-09-2007, 02:11 AM
On /test/test.html 'offeredparent' is undefined as the script runs before the table 'trade_bait' is rendered.
Put the script below the table.

DoubleJ
07-09-2007, 02:37 AM
test.html is updated but still no cigar...

DoubleJ

rwedge
07-09-2007, 04:02 AM
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.

DoubleJ
07-09-2007, 04:32 AM
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:



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum