473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DOM: how to append to node'd contents?


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

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

Jul 23 '05 #1
3 1860
*J Krugman* wrote:
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?


Not sure why you can't append another text node to the pre element?
E.g.:

function AppendText(strId, strTxt) {
var objNode = document.getElementById(strId);
if (objNode) {
var objTextNode = document.createTextNode(strTxt);
objNode.appendChild(objTextNode);
}
}
<pre id="myNode" onclick="AppendText('myNode', 'It was\r\nthe fact'
+' that the cave was in the middle of Islington and there wasn\'t\r\n'
+'a bus due for two million years.')">Life, the Universe, and Everything
for Sally
Chapter 1
The regular early morning yell of horror was the sound of Arthur Dent
waking up and suddenly remembering where he was. It wasn't just that
the cave was cold, it wasn't just that it was damp and smelly. </pre>
--
Andrew Urquhart
- FAQ: www.jibbering.com/faq/
- Archive: www.google.com/groups?q=comp.lang.javascript
- Contact me: http://andrewu.co.uk/contact/
Jul 23 '05 #2
DU
J Krugman wrote:
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
Jul 23 '05 #3
In <ce**********@news.eusc.inter.net> DU <dr*******@hotWIPETHISmail.com> writes:
J Krugman wrote:
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.


Thank you. That was very helpful.

jill

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marko Faldix | last post by:
Hello, I have got some standard problems still realized with MS xml 4. The following code runs on Windows machines with python and win32com installed. Could you use pyxml or 4suite to make...
4
by: Juliano Freitas | last post by:
How can i get the text between the <teste> tags?? >>> xml = """<root><teste> texto </teste></root>""" >>> from xml.dom import minidom >>> document = minidom.parseString(xml) >>> document...
3
by: Richard Lewis | last post by:
Hi there, I have an XML document which contains a mixture of structural nodes (called 'section' and with unique 'id' attributes) and non-structural nodes (called anything else). The structural...
2
by: DaRik | last post by:
Hi, I'm having some difficulties with a menu I'm making. I build up the menu through DOM. I append childnodes to a tree. 2 types of children are possible: url (a hyperlink) and sub (a submap). ...
8
by: jog | last post by:
Hi, I want to get text out of some nodes of a huge xml file (1,5 GB). The architecture of the xml file is something like this <parent> <page> <title>bla</title> <id></id> <revision> <id></id>...
2
by: James | last post by:
Hello everyone. I've been looking for examples on this: http://www.php.net/manual/en/functi...-appenddata.php but can't really find one. I've got a function that generates a return value in...
3
by: robert.oschler | last post by:
I have an AJAX driven page where I dynamically add rows to a known table on the page, based on the return document from the AJAX call, using DOM node methods (except for a small snippet of code,...
3
by: mich dobelman | last post by:
Currently my web server supports only php4. I want to know how to parse the xml and get the element value using dom. I thought they have similar function for getting element by id or tag name, so...
7
by: vunet.us | last post by:
I cannot append a node from XML into the HTML dom (textarea field) in IE. But I can for the text input html elements! Is anyone aware of this and what is the possible solution? Thanks.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.