[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-06-22
3
Medium Priority
?
481 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
1 Comment
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Several part series to implement Internet Explorer 11 Enterprise Mode
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
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…

825 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