Way(s) to dynamically change text content

M

michael

I have an html text string within a div, eg.:

<div id="example">Text text text</div>

I know its easy to change styles by using getElementById - for example:

document.getElementById("example").style.fontColor="#ff0000";

But what way(s) are there to change the actual text content itself on the
page dynamically, from "Text text text" to"Bla bla bla" ?

Can it be done with JS/CSS?
 
H

Hywel Jenkins

I have an html text string within a div, eg.:

<div id="example">Text text text</div>

I know its easy to change styles by using getElementById - for example:

document.getElementById("example").style.fontColor="#ff0000";

But what way(s) are there to change the actual text content itself on the
page dynamically, from "Text text text" to"Bla bla bla" ?

Can it be done with JS/CSS?

document.getElementById('example').innerHTML = 'New text';
 
M

Martin Honnen

michael said:
I have an html text string within a div, eg.:

<div id="example">Text text text</div>
But what way(s) are there to change the actual text content itself on the
page dynamically, from "Text text text" to"Bla bla bla" ?

It is possible to set the text, to cope with various browsers from IE 4,
5, 6, Netscape 6, 7, Mozilla, Opera 7, 8 and other DOM compliant
browsers you can use:


function setInnerText (elementId, text) {
var element;
if (document.getElementById) {
element = document.getElementById(elementId);
}
else if (document.all) {
element = document.all[elementId];
}
if (element) {
if (typeof element.textContent != 'undefined') {
element.textContent = text;
}
else if (typeof element.innerText != 'undefined') {
element.innerText = text;
}
else if (typeof element.removeChild != 'undefined') {
while (element.hasChildNodes()) {
element.removeChild(element.lastChild);
}
element.appendChild(document.createTextNode(text));
}
}
}


setInnerText('example', 'Kibology for all.')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top