DOM: how to append to node'd contents?

J

J Krugman

This question refers to the DOM. I would like to dynamically append
some small amount of text to the all-text content of a <pre> node.
I suppose that I could extract the existing content, and replace
the <pre> node in question with a new <pre> node that has the
extended content. But the existing content is longish, and it
seems to me wasteful to rewrite it all just to have a small amount
of text tacked on to the end. Is there a way to simply append the
new text to the node's contents?

TIA,

jill
 
D

DU

J said:
This question refers to the DOM. I would like to dynamically append
some small amount of text to the all-text content of a <pre> node.
I suppose that I could extract the existing content, and replace
the <pre> node in question with a new <pre> node that has the
extended content. But the existing content is longish, and it
seems to me wasteful to rewrite it all just to have a small amount
of text tacked on to the end. Is there a way to simply append the
new text to the node's contents?

TIA,

jill


W3C DOM 2 CharacterData appendData() method is what you're looking for
here and appendData() is supported by almost all browsers (MSIE 5+ Mac
and Windows, NS 6+, Mozilla 1.x, Firefox 0.5+, Safari 1.x, K-meleon
0.7+, Opera 7.x, Galeon 1.x, Konqueror 3.x, etc.) and is a W3C web
standard method.

Assuming you have:

<pre id="idPre"> ... </pre>

then

document.getElementById("idPre").childNodes[0].appendData(" some
additional text");

will do the trick.
You can use other DOM 2 CharacterData methods like insertData() to have
more control.

DU
 
J

J Krugman

In said:
W3C DOM 2 CharacterData appendData() method is what you're looking for
here and appendData() is supported by almost all browsers (MSIE 5+ Mac
and Windows, NS 6+, Mozilla 1.x, Firefox 0.5+, Safari 1.x, K-meleon
0.7+, Opera 7.x, Galeon 1.x, Konqueror 3.x, etc.) and is a W3C web
standard method.
Assuming you have:
<pre id="idPre"> ... </pre>

document.getElementById("idPre").childNodes[0].appendData(" some
additional text");
will do the trick.
You can use other DOM 2 CharacterData methods like insertData() to have
more control.

Thank you. That was very helpful.

jill
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top