SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    wrong child node count

    Code:
    function myInspector(DTname)
    {
    var chiren='';
    var DTname;
    var DDlist = document.getElementById(DTname);
    	if (DDlist.hasChildNodes())
    	{
    	var ch=DDlist.childNodes;
    	alert(ch.length);
    	}
    
    }
    This returns an inaccurate count of 1 .. what am I missing ?
    There should be a count of 5.
    Thanks

    Here is the HTML output ...
    Code:
    <dl >
    	
    <dt id="Ed">Ed</dt>
    
    
    
    
    <dd>Sendmail move to exchange </dd>
    
    
    <dd>RCCS / SAP support</dd>
    
    
    <dd>Home Directory Servers </dd>
    
    
    <dd>Privileged Account Logging</dd>
    
    
    <dd>SAP Support Group permissions</dd>
    
    
    <dd>AIX Printing </dd>
    	
    <dt id="Jeff">Jeff</dt>
    
    
    
    <dd>Server Sysbacks</dd>
    
    
    <dd>Nagios Monitoring</dd>
    
    
    <dd>Sap Support </dd>
    
    
    <dd>NIM Implementation/Maintenance</dd>
    	
    
    <dt id="Seth">Seth</dt>
    
    
    
    
    <dd>Perl 5.8.8 AIX standardization</dd>
    
    
    <dd>JUMS Support</dd>
    
    
    <dd>User clean up for Vintella</dd>
    
    <dd>Linux Support and Standardization</dd>
    
    
    <dd>Day Call Support</dd>
    
    </dl>
    Last edited by simply seth; Sep 21, 2006 at 13:22. Reason: Omitted Info

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excuse me. using html tagnames for identifiers is unusual.
    Last edited by mrhoo; Sep 22, 2006 at 09:56.

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're getting the DT, which has only one child node (a Text node). DD is not a child node of a DT. It's the child node of a DL, and a sibling of the DT.

    If you want to know the number of DD elements following a specific DT, you need to do something like this:
    Code:
    function myInspector(DTName)
    {
        var dt = document.getElementById(DTName);
        var ddCount = 0;
    
        for (var n = dt.nextSibling; n != null; n = n.nextSibling) {
            if (n.nodeType == 1) {
                if (n.nodeName == "DD") {
                    ++ddCount;
                } else {
                    break;
                }
            }
        }
    
        alert("This DT is followed by " + ddCount + " DD node(s)");
    }
    Last edited by AutisticCuckoo; Sep 22, 2006 at 00:00.
    Birnam wood is come to Dunsinane

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi...
    please can you explain me this for loop..
    for (var n = dt.nextSibling; n != 0; n = n.nextSibling)

    thanx in advance...

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The dt variable is originally set to the DT Element node with the specified ID. The loop begins by assigning the next sibling of the DT node to the variable n. That will be a whitespace Text node or a DD Element node.

    For each iteration of the loop, the n variable will be moved on to its next sibling. That will be a whitespace Text node or a DD or DT Element node. Text nodes are ignored. DD nodes are counted, and when some other element than DD is encountered, the loop is terminated.

    The exit condition (which should be n != null, not 0; my mistake) is to handle the case where the sought-after DT is the last DT in the definition list. That means a DD Element node or a whitespace Text node will be the last child node of the DL. The last child node has a null value for its nextSibling property.

    In the body of the loop, we're first checking the nodeType property. That will be 1 for Element nodes, which is the only type we're interested in. (Text nodes will have nodeType==3.)

    For each Element node we encounter, we check the nodeName property to see what element type it is. If it's a DD we increment the counter. If it's something else (can only be a DT, really) we've found all DDs that follow the specified DT, so we exit the loop.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •