473,385 Members | 1,372 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.

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.getElementById("myUL");
alert ("childNodes[0].tagName: " +
document.getElementById("myUL").childNodes[0].tagName + "\n" +
"childNodes[0].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[0].firstChild.nodeValue + "\n" +
"childNodes[1].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[1].firstChild.nodeValue + "\n" +
"childNodes[2].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[2].firstChild.nodeValue + "\n" +
"childNodes[3].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[3].firstChild.nodeValue + "\n" +
"childNodes[4].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[4].firstChild.nodeValue + "\n" +
"childNodes[5].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[5].firstChild.nodeValue);
</script>
</body>
</html>
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++

Firefox says document.getElementById("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.getElementById("myUL");
alert ("nodeType " + document.getElementById("myUL").nodeType + "\n" +
"tagName " + document.getElementById("myUL").tagName + "\n" +
"length " + document.getElementById("myUL").length + "\n" +
"innerHTML \n" + document.getElementById("myUL").innerHTML + "\n" +
"childNodes[0].tagName " +
document.getElementById("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 1761
Disregard the variable definition 'var ulist =
document.getElementById("myUL");'
That was part of another test.

"Joe Cox" <jc**@kornet.net> 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.getElementById("myUL");
alert ("childNodes[0].tagName: " +
document.getElementById("myUL").childNodes[0].tagName + "\n" +
"childNodes[0].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[0].firstChild.nodeValue + "\n"
+
"childNodes[1].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[1].firstChild.nodeValue + "\n"
+
"childNodes[2].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[2].firstChild.nodeValue + "\n"
+
"childNodes[3].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[3].firstChild.nodeValue + "\n"
+
"childNodes[4].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[4].firstChild.nodeValue + "\n"
+
"childNodes[5].firstChild.nodeValue: " +
document.getElementById("myUL").childNodes[5].firstChild.nodeValue);
</script>
</body>
</html>
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++

Firefox says document.getElementById("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.getElementById("myUL");
alert ("nodeType " + document.getElementById("myUL").nodeType + "\n" +
"tagName " + document.getElementById("myUL").tagName + "\n" +
"length " + document.getElementById("myUL").length + "\n" +
"innerHTML \n" + document.getElementById("myUL").innerHTML + "\n" +
"childNodes[0].tagName " +
document.getElementById("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.getElementById("myUL");
alert ("childNodes[0].tagName: " +
document.getElementById("myUL").childNodes[0].tagName + "\n" +
"childNodes[0].firstChild.nodeValue: " +


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. getElementsByTagName('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*******@yahoo.de> wrote in message
news:42***********************@newsread4.arcor-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.getElementById("myUL");
alert ("childNodes[0].tagName: " +
document.getElementById("myUL").childNodes[0].tagName + "\n" +
"childNodes[0].firstChild.nodeValue: " +


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. getElementsByTagName('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.getElementById("myUL");
alert ("childNodes[0].tagName: " +
document.getElementById("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
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....
4
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...
1
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">...
2
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"...
7
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...
2
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...
1
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
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...
3
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...
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.