473,320 Members | 1,848 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,320 software developers and data experts.

Getting Entire Node

If I had a span tag like:

<span id='msg_2'>
There is a bunch of text <a href="http://www.someurl.com"> here </a>. And
some others <a href="http://www.otherurl.com">here</a>. Good luck!
</span>

And it contains other tags in it.

If I use: document.getElementById('msg_2').firstChild.nodeVa lue
then I just get: "There is a bunch of text".

I'd like to get the total node contained between the span tag.

Anyone know how I can get this?

Mike
Jul 23 '05 #1
4 1454
This works:

document.getElementById('msg_2').innerHTML

But is it correct?

"Michael Hill" <hi****@charter.net> wrote in message
news:10*************@corp.supernews.com...
If I had a span tag like:

<span id='msg_2'>
There is a bunch of text <a href="http://www.someurl.com"> here </a>. And
some others <a href="http://www.otherurl.com">here</a>. Good luck!
</span>

And it contains other tags in it.

If I use: document.getElementById('msg_2').firstChild.nodeVa lue then I
just get: "There is a bunch of text".

I'd like to get the total node contained between the span tag.

Anyone know how I can get this?

Mike

Jul 23 '05 #2
Michael Hill wrote:
This works:

document.getElementById('msg_2').innerHTML

But is it correct?

[...]

If you want the HTML contained between the <span> </span> tags, yes.

When you do:

document.getElementById('msg_2').innerHTML = 'some text'

the entire content between the tags is replaced by 'some text'. You
can put HTML in there to:

document.getElementById('msg_2').innerHTML = '<b>some<br>text</b>'

and so on.

But if you want to manipulate the contents using DOM, you need to do
something like:

var theNodes = document.getElementById('msg_2').childNodes

'theNodes' will be a collection of the nodes of 'msg_2'. Note that it
will be a collection of the direct children of msg_2, you need other
DOM methods to get at lower nodes:

theNodes[0].childNodes[0]

will return the first child of the first child of msg_2.

Fred.
Jul 23 '05 #3

"Fred Oz" <oz****@iinet.net.auau> wrote in message
news:41**********************@per-qv1-newsreader-01.iinet.net.au...
Michael Hill wrote:
This works:

document.getElementById('msg_2').innerHTML

But is it correct?

[...]

If you want the HTML contained between the <span> </span> tags, yes.

When you do:

document.getElementById('msg_2').innerHTML = 'some text'

the entire content between the tags is replaced by 'some text'. You
can put HTML in there to:

document.getElementById('msg_2').innerHTML = '<b>some<br>text</b>'

and so on.

But if you want to manipulate the contents using DOM, you need to do
something like:

var theNodes = document.getElementById('msg_2').childNodes

'theNodes' will be a collection of the nodes of 'msg_2'. Note that it
will be a collection of the direct children of msg_2, you need other
DOM methods to get at lower nodes:

theNodes[0].childNodes[0]

will return the first child of the first child of msg_2.

Fred.


Fred,

That is what I was thinking. Appreciate you.

Mike
Jul 23 '05 #4
document.getElementById returns a ref to the
entire node including its childNodes which
can be referenced through the childNodes
collection of the DOM. Read up on DOM Level 2.
<http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=25>
You will find references and examples.
Also different browsers will treat spaces in
your markup as separate text nodes so be
careful with your tags and leaving spaces.
I'm no expert on this but this should
help. Tested in IE6. Just run it and then
inspect the code. Watch out for word wrap!
Jimbo
<HTML>
<HEAD><TITLE></TITLE>
</HEAD>
<BODY>
<span id='msg_2'>There is a bunch of text
<a href="http://www.someurl.com">here</a>
.. And some others
<a href="http://www.otherurl.com">here</a>
.. Good luck!</span>
<script type="text/javascript">
var obj = document.all?msg_2:document.getElementById('msg_2' );
var len = obj.childNodes.length;
alert('the node msg_2 has '+
len +' childNodes');
for (var i=0; i<len; i++) {
var el = obj.childNodes[i];
var nType = el.nodeType;
if( nType== 1) { // element
if( el.childNodes.length ) {
var more = 'with ' +
el.childNodes.length +
' childNodes\n' +
'obj.childNodes[' + i +
'].firstChild.nodeValue = ' +
el.firstChild.nodeValue;
}
alert('obj.childNodes['+i+'] is a <' +
el.tagName +
'> element\n'+ more);
}
if(nType == 3) { // text node
alert('obj.childNodes['+i+'] is a text node\n'+
'obj.childNodes[' +i+ '].nodeValue = ' +
el.nodeValue);
}
}
</script>
</BODY>
</HTML>
Jul 23 '05 #5

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

Similar topics

7
by: F. Da Costa | last post by:
Hi, I' looking to retrieve ProdName1 form the <tr> below. <tr id="1-1-1" class="even"> <td> <div class="tier4"> <a href="#" class="leaf"></a> ProdName1 </div>
6
by: Affan Syed | last post by:
Hi, I am getting this weird problem. I know what i am doing is strange.. i am using C++ vectors and fopen, but for some reason if i used ofstream in the similar scenario it would give me errors....
4
by: leodippolito | last post by:
Hello sirs, I am trying to send a POST request to a webservice on the click of a button. This will return me an XML document with a list of combo box items. The problem: in FIREFOX, when the...
7
by: Steven Bethard | last post by:
How do I make sure that my entire string was parsed when I call a pyparsing element's parseString method? Here's a dramatically simplified version of my problem: py> import pyparsing as pp py>...
5
by: DarthDaddy | last post by:
I hope to explain this properly. Here is a sample section of a file I am working with: <achievements> <achievement> <item name="COURSE_COMPLETION_DATE">19930630</item> <item...
3
by: Eroc | last post by:
I'm new to XML files so I'm kinda lost here. I found some example code on reading an XML file. My objective is simple. Read the whole XML file into memory. Here is part of my code: Private...
4
by: R.Manikandan | last post by:
Hi In my code, one string variable is subjected to contain more amount of characters. If it cross certain limit, the string content in the varabile is automatically getting truncated and i am...
2
by: Bardo | last post by:
Hi all, Does anyone know if it is possible, and if so how, to perform validation of a simple non XML string against certain XSD restrictions, without having the entire XML document to validate...
0
by: ullner | last post by:
I have an XML fil that looks like this: <Environment> <AreaOfInterest> <Name>ScenarioMap</Name> <UpperRight> <GDC> <Latitude>-179</Latitude> ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.