[Javascript] Getting contents of an element in a node list
I've read in an XML document and have got a node list using getelementsbytagname.
This works ok as the length of the nodelist is 18, which is right having checked the xml document i'm reading in.
Trouble is, i can't workout how to access the contents of the element, i.e.
<title mode="escaped" type="text/html">Long time no see</title>
I've tried the following, but nothing works. (i'm testing using the alert statement).
MyNodeList.item(0).value
MyNodeList.item(0).innerHTML
I've also tried accessing other elements besides 0.
When i do alert(MyNodeList.item(0)) it correctly displays a message box containing [Object Element], so i know the element's there!
Any help would be greatly appreciated!
Re: [Javascript] Getting contents of an element in a node list
What happends when you do:
alert(MyNodeList.item(0).childNodes[1].firstChild.nodeValue);
Re: [Javascript] Getting contents of an element in a node list
That is because the node contains another node. A text node object which is dreived from a node object. To access it you need to to do the following:
Code:
MyNodeList.item(0).childNodes[0].nodeValue
Re: [Javascript] Getting contents of an element in a node list
Cool that works, thanks Adam.
Just a quick question though, am i right in thinking that you can only get the innerHTML of a leaf element then?? If so, what if i want just a branch of the document tree???