473,770 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i don't understand how DOM counts up divRef.childNod es.length

This weekend I decided to play around with Javascript a little and try
to teach myself some things about AJAX and DOM. I've been doing
experiments on this page:

http://www.publicdomainsoftware.org/ajaxExperiment.htm

If you go there and click in any box, you'll get some controls. If you
then click "Add an Image" you'll have the chance to add an image. But
how many child elements are there in the box that communicates with
you? I count 4 items at most:

<div id="inputDiv">
<textarea id="inputBox" class="wonderfu l"></textarea><br />
<input id="submitButto n" type="button" value="Submit"
onClick="getInp ut();">
<input type="button" value="Cancel"
onClick="hideDi v('communicatio nBox');">
</div>

And yet, in the javascript code, when I try to get the number of child
elements, I get 8 in FireFox and 7 in IE. This is the code that
captures the info and I use alert() to make it visible:

var divRef = document.getEle mentById("input Div");
var firstChildNode = divRef.firstChi ld;
alert(divRef.ch ildNodes.length );
if (divRef.childNo des.length > 6) {
divRef.removeCh ild(firstChildN ode);
firstChildNode = divRef.firstChi ld;
}

Why am I getting 7 and 8 instead of 4? I don't get how the DOM is
counted.

Feb 7 '06 #1
3 3555
Jake Barnes wrote:

<div id="inputDiv">
<textarea id="inputBox" class="wonderfu l"></textarea><br />
<input id="submitButto n" type="button" value="Submit"
onClick="getInp ut();">
<input type="button" value="Cancel"
onClick="hideDi v('communicatio nBox');">
</div>

Why am I getting 7 and 8 instead of 4? I don't get how the DOM is
counted.

Check the node types, you will see number of TEXT nodes, representing
the whitespace.

--
Ian Collins.
Feb 7 '06 #2
Jake Barnes wrote:
This weekend I decided to play around with Javascript a little and try
to teach myself some things about AJAX and DOM. I've been doing
experiments on this page:

http://www.publicdomainsoftware.org/ajaxExperiment.htm

If you go there and click in any box, you'll get some controls. If you
then click "Add an Image" you'll have the chance to add an image. But
how many child elements are there in the box that communicates with
you? I count 4 items at most:
[...]
Why am I getting 7 and 8 instead of 4? I don't get how the DOM is
counted.


As Ian said, check the nodes - use the DOM inspector.

Gecko browsers will insert #text nodes to preserve whitespace in the
source document (after collapsing whitespace first).

<table>

<!-- this tr has one child - the td -->
<tr onclick="alert( this.childNodes .length);"><td> Show kids</td></tr>

<!-- this tr has two children - a #text and a td -->
<tr onclick="alert( this.childNodes .length);">
<td>Show kids</td></tr>

<!-- this tr has three children - a #text, td and #text -->
<tr onclick="alert( this.childNodes .length);">
<td>Show kids</td>
</tr>

</table>
But in IE all of them have just one child, the TD.

--
Rob
Feb 7 '06 #3
Jake Barnes wrote :

[snipped]

Why am I getting 7 and 8 instead of 4? I don't get how the DOM is
counted.

Everything is explained in:

Whitespace in the DOM
http://developer.mozilla.org/en/docs...ace_in_the_DOM

Gérard
--
remove blah to email me
Feb 7 '06 #4

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

Similar topics

7
3359
by: adam | last post by:
i'm working on a portion of a CMS that allows content-admins to browse a product list, and add individual products into the taxonomy by clicking checkboxes next to categories they might belong in. since the taxonomy is a rather long list, i'm hiding and showing divs for the secondary and tertiary links, so when a user clicks on the checkbox for the parent category, the children appear in a second (and third) div, with checkboxes of their...
2
2568
by: chuck | last post by:
Hi, I am modifying some code from here http://www.quirksmode.org/dom/domform.html I have a div 'readroot' that I clone. I change the change the id and name of the childnodes of 'readroot' to the original name plus a number(counter). The problem is I have i have a div 'serials' inside 'readroot' and the childnodes of
3
8495
by: Q1tum | last post by:
Hi all, I have a problem with getting the amount of childs in a XML structure, the strucure is somewhat like the following: <?xml version="1.0" encoding="iso-8859-1"?> <cms> <num>21</num> <xmlnames> <field>id</field>
12
4493
by: Q1tum | last post by:
Hi all, I got a problem with childNodes.length, I use the following XML for my guestbook: <?xml version="1.0" encoding="ISO-8859-1"?> <guestbook> <entry> <from>Q1tum</from> <mail>kuukelekuu at gmail dot com</mail>
5
22509
by: Moses | last post by:
HI The Value for childNodes.length differs with mozilla and IE Is it problem with my coding..... I could not under stood............. The following is the details
7
13474
by: Tarik Monem | last post by:
Here's the latest snag in the code: I was able to retrieve data from the most of the xml files except for the xml files that contain attributes but I have yet to find a tutorial that explains the retrieval of the attributes of a node. Here's the XML: <episode> <title>Episode 101 - DECEMBER 5TH, 2006</title>
3
3011
by: praveensewak | last post by:
I have written an AJAX script: for (j=0;j<xml.childNodes.length;j++){ /*** CODE HERE ****/ } In IE, i get the corrent code rendered, BUT in Firefox, i only get the first entry!!! When i alert(xml.childNodes.length) in Firefox, i ALWAYS get 1!!!! it works fine in IE, Opera, etc.... Please someone (anyone!) help!!!
2
3092
by: willyWEB66 | last post by:
Hi everyone, I have this code in javascript where the XML file is loaded and displayed to an html using XSLT. It works fine in IE but not in Firefox. My problem is in the looping to the childNodes. It seems that Firefox does not recognize this line of code >> xmlDoc.getElementsByTagName("urlValue").childNodes.nodeValue I'm not sure if this is the line that is not working, or the lines before it. I'm looping to the childNodes to compare to...
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
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
10237
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
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5326
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
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
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

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.