Forum Moderators: open

Message Too Old, No Replies

Changing a text nodes content onload

New to javascript

         

druidjaidan

12:29 am on Jul 27, 2007 (gmt 0)

10+ Year Member



I have the following snippet of javascript I wrote to change the content of a <span>

document.getElementsByTagName('body')[0].onload = pageload;

function pageload() {
var sysPrice = 0;
var pricetext;
sysPrice = generateprice();
checkcompat();

pricetext = document.getElementById('sysprice');
pricetext.firstChild.data = ("$" + sysPrice);
}

Unless I'm missing something what I'm hoping to accomplish is to first link the body tag's onload event to the function pageload. Then once the event is triggered (onload) run the function. This should then update the textnode (the child of a span) to be the price calculated by generateprice().

It's not working...it might not even be running the function at all...so what has gone wrong?

*I should note that I'm using firefox with the firebug extension, it indicates that the onload event is registered with body...but I even hid an alert() in the function for a while and it never popped.

bear

7:11 am on Jul 27, 2007 (gmt 0)

10+ Year Member



Hi,

Here is a little function that takes an 'id' for a DIV or SPAN or A and changes the text it dynamically.

e.g. textmod('myid','New Text')

It works cross browser.

function textmod (elnam, newtext) {
// get reference to the element
var ttext = document.getElementById(elnam);
var new_txt = document.createTextNode(newtext);
ttext.replaceChild(new_txt, ttext.childNodes[0]);
}

Cheers,

daveVk

7:11 am on Jul 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try changing

document.getElementsByTagName('body')[0].onload = pageload;

to

window.onload = pageload;

This statement may be executed prior to page loading in which case 'body' or other tags for that matter, are not yet known.