Solved

Please explain difference between IE and Firefox in dom .childNodes length values.

Posted on 2006-06-22
3
450 Views
Last Modified: 2008-01-16
Here is a test html file.  (see below).  In IE it returns the number of child nodes for the row as 3, which is what I expected.  In firefox it returns the number of children as 7, which appears to be the number of all descendents, not just the number of children.  Is there an easy way of getting the number of children that will work in both IE and firefox?

==== code below ====
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
      <title>Untitled</title>
</head>

<body>
<table id="t" border="1">
<tr id="row1">
<td id="cell1">cell 1</td>
<td id="cell2"><div id="div1">cell 2 in div</div></td>
<td id="cell3"><div id="div2">cell 3 in div</div></td>
</tr>
</table>

<script type="text/javascript">var tr = document.getElementById("row1")
            document.write("number of childs:"+tr.childNodes.length)
</script>
</body>
</html>
0
Comment
Question by:Bob_j_Wallace
3 Comments
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 125 total points
ID: 16964817
That's because IE ignores the text nodes that separate your <td> elements.
This script shows the node types (3 = text node):

<script type="text/javascript">
  var tr = document.getElementById("row1")
  document.write("number of childs:"+tr.childNodes.length)
  var children = tr.childNodes;
  for (var i = 0; i < children.length; i++) {
    document.write("<br>childs node type:" + children[i].nodeType);
  }
</script>

remove the space chars between your <td> and you'll get the same results in all browsers:

<table id="t" border="1">
<tr id="row1"><td id="cell1">cell 1</td><td id="cell2"><div id="div1">cell 2 in div</div></td><td id="cell3"><div id="div2">cell 3 in div</div></td></tr>
</table>

You can use the DOM inspector in FF (menu tools/DOM inspector).  It will show you the text nodes.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After uninstalling Opera browser (for example ver. 10.63), your attempts to open a web page by clicking on a URL link may fail with an error message.  The error is "This operation has been canceled due to restrictions in effect on this computer. Ple…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question