Please help me understand this DOM thing

Discussion in 'XML' started by RC, Mar 4, 2008.

  1. RC

    RC Guest

    I want to recursively go through to an element node.

    Here are the simple lines

    <a href="#" onClick="startANode('ul1')">Recursive Read</a>

    <ul id="ul1">
    <li>line 1<b>bold 1<i>italic 1<u>under 1</u></i></b></li>
    <li><b><i><u>line 2</u></i></b></li>
    <li><b><i><u>line 3</u></i></b></li>
    <li><b><i><u>line 4</u></i></b></li>
    <li><b><i><u>line 5</u></i></b></li>
    </ul>


    But my java script only goes through the line 1. It never
    goes through line 2 to line 5. Can you help me find the problem
    in my script?

    Thank Q in advance!

    BTW, why DOM has firstChild, lastChild but doesn't have nextChild?

    Here is the java script

    <script type="text/javascript">

    function startANode(id) {
    var thisNode = document.getElementById(id);
    recursiveRead(thisNode);
    }
    function recursiveRead(node) {
    //alert(id);
    if (! node.hasChildNodes()) {
    alert(
    "Node name = " + node.nodeName + "\n" +
    "Node value = " + node.nodeValue + "\n" +
    "Node type = " + node.nodeType + "\n" +
    "Node id = " + node.id + "\n" +
    "Node data = " + node.data + "\n"
    );
    } else {
    nodeList = node.childNodes;
    alert(nodeList.length);
    for (var i = 0; i < nodeList.length; i++) {
    aNode = nodeList;
    recursiveRead(aNode);
    }
    }
    }
    </script>
     
    RC, Mar 4, 2008
    #1
    1. Advertising

  2. RC wrote:

    > BTW, why DOM has firstChild, lastChild but doesn't have nextChild?


    It has nextSibling and previousSibling so you could access the first
    child with var child = node.firstChild, then the next child with
    child.nextSibling. And you can loop through the childNodes collection.


    > function recursiveRead(node) {
    > //alert(id);
    > if (! node.hasChildNodes()) {
    > alert(
    > "Node name = " + node.nodeName + "\n" +
    > "Node value = " + node.nodeValue + "\n" +
    > "Node type = " + node.nodeType + "\n" +
    > "Node id = " + node.id + "\n" +
    > "Node data = " + node.data + "\n"
    > );
    > } else {
    > nodeList = node.childNodes;
    > alert(nodeList.length);
    > for (var i = 0; i < nodeList.length; i++) {
    > aNode = nodeList;
    > recursiveRead(aNode);


    It is generally a good idea to declare variables and use local
    variables, it becomes mandatory once you want to write recursive
    functions so use

    else {
    var nodeList = node.childNodes;
    alert(nodeList.length);
    for (var i = 0; i < nodeList.length; i++) {
    var aNode = nodeList;
    recursiveRead(aNode);
    }

    and your function should do what you want.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 4, 2008
    #2
    1. Advertising

  3. RC

    RC Guest

    Martin Honnen wrote:

    > It is generally a good idea to declare variables and use local
    > variables, it becomes mandatory once you want to write recursive
    > functions so use



    Thank Q very very much Martin! Local variable, local variables.
    I remember that in future.
     
    RC, Mar 4, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    483
    Joerg Jooss
    Aug 31, 2005
  2. KK
    Replies:
    2
    Views:
    731
    Big Brian
    Oct 14, 2003
  3. Zam
    Replies:
    1
    Views:
    267
    Mark Schupp
    Mar 14, 2005
  4. Jake Barnes
    Replies:
    2
    Views:
    139
    GĂ©rard Talbot
    Feb 7, 2006
  5. RC
    Replies:
    2
    Views:
    117
Loading...

Share This Page