473,769 Members | 5,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing List Nodes

I appologize for the novice question. I believe in am using standard ECMA.
Can someone explain to me why the following script works in IE but does not
work in Firefox, Mozila, or Opera?

+++++++++++++++ +++++++++++++++ ++++
<html>
<head>
</head>
<body>
<ul id="myUL">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
<li>List Item 5</li>
<li>List Item 6</li>
</ul>
<script type="text/javascript">
var ulist = document.getEle mentById("myUL" );
alert ("childNodes[0].tagName: " +
document.getEle mentById("myUL" ).childNodes[0].tagName + "\n" +
"childNodes[0].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[0].firstChild.nod eValue + "\n" +
"childNodes[1].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[1].firstChild.nod eValue + "\n" +
"childNodes[2].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[2].firstChild.nod eValue + "\n" +
"childNodes[3].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[3].firstChild.nod eValue + "\n" +
"childNodes[4].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[4].firstChild.nod eValue + "\n" +
"childNodes[5].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[5].firstChild.nod eValue);
</script>
</body>
</html>
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++

Firefox says document.getEle mentById("myUL" ).childNodes[0].firstChild 'has
no properties'.

Then I tried the following script:

+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +
<html>
<head>
</head>
<body>
<ul id="myUL">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
<li>List Item 5</li>
<li>List Item 6</li>
</ul>
<script type="text/javascript">
var ulist = document.getEle mentById("myUL" );
alert ("nodeType " + document.getEle mentById("myUL" ).nodeType + "\n" +
"tagName " + document.getEle mentById("myUL" ).tagName + "\n" +
"length " + document.getEle mentById("myUL" ).length + "\n" +
"innerHTML \n" + document.getEle mentById("myUL" ).innerHTML + "\n" +
"childNodes[0].tagName " +
document.getEle mentById("myUL" ).childNodes[0].tagName + "\n");
</script>
</body>
</html>
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++

The output from that was:

+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++
nodeType 1
tagName UL
length undefined
innerHTML
<LI>List Item 1</LI>
<LI>List Item 2</LI>
<LI>List Item 3</LI>
<LI>List Item 4</LI>
<LI>List Item 5</LI>
<LI>List Item 6</LI>
childNodes[0].tagName undefined
+++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++

Why is childNodes[0] undefined?

Thanks in advance,
Joe Cox
Jul 23 '05 #1
4 1783
Disregard the variable definition 'var ulist =
document.getEle mentById("myUL" );'
That was part of another test.

"Joe Cox" <jc**@kornet.ne t> wrote in message
news:db******** **@news2.kornet .net...
I appologize for the novice question. I believe in am using standard ECMA.
Can someone explain to me why the following script works in IE but does not
work in Firefox, Mozila, or Opera?

+++++++++++++++ +++++++++++++++ ++++
<html>
<head>
</head>
<body>
<ul id="myUL">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
<li>List Item 5</li>
<li>List Item 6</li>
</ul>
<script type="text/javascript">
var ulist = document.getEle mentById("myUL" );
alert ("childNodes[0].tagName: " +
document.getEle mentById("myUL" ).childNodes[0].tagName + "\n" +
"childNodes[0].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[0].firstChild.nod eValue + "\n"
+
"childNodes[1].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[1].firstChild.nod eValue + "\n"
+
"childNodes[2].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[2].firstChild.nod eValue + "\n"
+
"childNodes[3].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[3].firstChild.nod eValue + "\n"
+
"childNodes[4].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[4].firstChild.nod eValue + "\n"
+
"childNodes[5].firstChild.nod eValue: " +
document.getEle mentById("myUL" ).childNodes[5].firstChild.nod eValue);
</script>
</body>
</html>
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ ++

Firefox says document.getEle mentById("myUL" ).childNodes[0].firstChild 'has
no properties'.

Then I tried the following script:

+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +
<html>
<head>
</head>
<body>
<ul id="myUL">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
<li>List Item 5</li>
<li>List Item 6</li>
</ul>
<script type="text/javascript">
var ulist = document.getEle mentById("myUL" );
alert ("nodeType " + document.getEle mentById("myUL" ).nodeType + "\n" +
"tagName " + document.getEle mentById("myUL" ).tagName + "\n" +
"length " + document.getEle mentById("myUL" ).length + "\n" +
"innerHTML \n" + document.getEle mentById("myUL" ).innerHTML + "\n" +
"childNodes[0].tagName " +
document.getEle mentById("myUL" ).childNodes[0].tagName + "\n");
</script>
</body>
</html>
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++

The output from that was:

+++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++
nodeType 1
tagName UL
length undefined
innerHTML
<LI>List Item 1</LI>
<LI>List Item 2</LI>
<LI>List Item 3</LI>
<LI>List Item 4</LI>
<LI>List Item 5</LI>
<LI>List Item 6</LI>
childNodes[0].tagName undefined
+++++++++++++++ +++++++++++++++ +++++++++++++++ ++++++++

Why is childNodes[0] undefined?

Thanks in advance,
Joe Cox

Jul 23 '05 #2


Joe Cox wrote:
I appologize for the novice question. I believe in am using standard ECMA.
Can someone explain to me why the following script works in IE but does not
work in Firefox, Mozila, or Opera? <ul id="myUL">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
<li>List Item 5</li>
<li>List Item 6</li>
</ul>
<script type="text/javascript">
var ulist = document.getEle mentById("myUL" );
alert ("childNodes[0].tagName: " +
document.getEle mentById("myUL" ).childNodes[0].tagName + "\n" +
"childNodes[0].firstChild.nod eValue: " +


The difference is how whitespace in the original source is modelled in
the DOM, Mozilla and Opera include whitespace as text nodes in the DOM
while IE in its HTML DOM does not meaning that childNodes[0] in IE for
instance is an element node while in Mozilla it is a text node.
So don't use the childNodes collection if you are only interested in
element nodes, unless you check which nodeType you have. Often it is
easier to use e.g. getElementsByTa gName('li') on the container element.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3
This had been bugging me for days. Thanks for the quick and clear
explanation.
"Martin Honnen" <ma*******@yaho o.de> wrote in message
news:42******** *************** @newsread4.arco r-online.net...


Joe Cox wrote:
I appologize for the novice question. I believe in am using standard
ECMA. Can someone explain to me why the following script works in IE but
does not work in Firefox, Mozila, or Opera?

<ul id="myUL">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
<li>List Item 5</li>
<li>List Item 6</li>
</ul>
<script type="text/javascript">
var ulist = document.getEle mentById("myUL" );
alert ("childNodes[0].tagName: " +
document.getEle mentById("myUL" ).childNodes[0].tagName + "\n" +
"childNodes[0].firstChild.nod eValue: " +


The difference is how whitespace in the original source is modelled in the
DOM, Mozilla and Opera include whitespace as text nodes in the DOM while
IE in its HTML DOM does not meaning that childNodes[0] in IE for instance
is an element node while in Mozilla it is a text node.
So don't use the childNodes collection if you are only interested in
element nodes, unless you check which nodeType you have. Often it is
easier to use e.g. getElementsByTa gName('li') on the container element.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #4
Joe Cox wrote:
I appologize for the novice question. I believe in am using standard
ECMA. Can someone explain to me why the following script works in IE
but does not work in Firefox, Mozila, or Opera?


Try cutting your script down a bit and it should all become obvious:

<html>
<head>
</head>
<body>
<ul id="myUL">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
<li>List Item 5</li>
<li>List Item 6</li>
</ul>
<script type="text/javascript">
var ulist = document.getEle mentById("myUL" );
alert ("childNodes[0].tagName: " +
document.getEle mentById("myUL" ).childNodes[0]);
</script>
</body>
</html>

Mozilla puts a text node for the whitespace between the <ul> and the <li>
tags. IE throws away the text nodes.
Jul 23 '05 #5

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

Similar topics

303
17755
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
4
2461
by: Erik Moore | last post by:
I am both producing and parsing an xml document that needs to be validated against a schema. I wanted some consumers of the document to have the option of not performing a validation, so I left the nodes in the instance unqualified. An example of the document is below: <?xml version="1.0" encoding="utf-8" ?> <filingreceipt xmlns="http://www.disclosureusa.org/filingreceipt.xsd"> <cpofilingnumber>TX2004043037501</cpofilingnumber>...
1
6135
by: Venkat Chellam | last post by:
I have a XML file like as follow <?xml version="1.0" encoding="UTF-8"?> <NeuStarLSRReport> <NeuStarHeader fromDate="09/10/04" toDate="09/13/04" RequestsReceived ="43" ResponsesSent="50"> </NeuStarHeader> <NeuStarReqestList> <NeuStarRequestRecieved pon="IDT2345611" ver="AA" dateReceived="09/10/04" />
2
2120
by: fizzy | last post by:
i am fetching an xml document with the following structure: <?xml version="1.0" encoding="UTF-8"?> <DTCResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:/ows/aws/1.2 http://www.overture.com/schema/dtc/1.2/dtc_response.xsd" xmlns="urn:/ows/aws/1.2" success="true"> <ActionsResponse accountId="123"> <GetAccountIdsResponse success="true"> <Account id="123" market="UK" /> ....
7
1719
by: ateale | last post by:
Hey Guys, I am discovering the awesomeness that is XML. I use an application called Final Cut Pro for editing video. The app is able to export is projects as XML. I am trying to develop a script to read that script and build a list of the files that are listed in the XML. The projects' imported files are enclosed in the 'pathurl' tag in the XML file. It is nearly working but I just wanted to see what you guys think of the manner I...
2
2668
by: nicky123 | last post by:
Hi everyone, This is a brief description that I have provided for parsing & displaying an XML document using DOM API. Please feel free to post your own comments & views regarding this discussion. Thank you. The first step of parsing an XML document is to import the DOM API related classes such as :- java.io.* which contains all the interfaces to perform an I/O operation. org.xml.sax.* which contains all the interfaces...
1
1737
by: bruce | last post by:
Hi Paul... Thanks for the reply. Came to the same conclusion a few minutes before I saw your email. Another question: tr=d.xpath(foo) gets me an array of nodes.
0
1426
by: dogatemycomputer | last post by:
I'm new to C#. I am using the following C# method to populate a TreeView control. If you call this method once during the life of form then it works perfectly. If you call the method a second time (without closing and reloading the app) then the process list populates the TreeView control with varying degrees of success. Sometimes the process name shows up (or not), threads that can load modules are red while others are not and...
3
1694
by: raghudr | last post by:
Hi all, I am parsing a .xml file.My main intention is to retrieve the name value of node "Signal":- "Name Value" which is "rag". i want to store only the <signal"name value" that is only "rag" ,"rock","yahoo"to my list. I do not want to add any <subsignals"name value" to the list.i want
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.