Jump to content

changing text dynamically


Recommended Posts

I want to change the text of an html element when a javascript function is called. Let's say my initial html contains<div id="title"> <b>Little house on the Priarie</b> </div>How do I progammatically change the text of this to someother title? Thanks

Link to comment
Share on other sites

It's not part of the W3C DOM, but i'd say it's the best supported and yes the most preferred way of text change.

I think it depends of what we need to get changed, only value of one text node or array of html tags.innerHTML is very good if there is many tags or should I say nodes. But if only need is change text value of one element DOM is not bad at all.innerHTML:
document.getElementById('id').innerHTML = 'gerbils are not rats!';


document.getElementById('id').firstChild.nodeValue = 'me tarzan you jane?';

OK, with DOM firstChild must exists, innerHTML wrotes is there nodes inside of element or not,but that is not a big problem, I think.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...