Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    Resolved nextSibling not working.

    Why wont this work?
    Code:
    var getNextSibling = document.getElementById("someElement").nextSibling;
    	if (getNextSibling = null)
    	{
    	 alert("SORRY NO SIBLINGS");
    	}
    	else
    	{
    	alert(getNextSibling);
    	}
    Last edited by ubh; 09-25-2008 at 05:45 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    if (getNextSibling = null)
    because you're assigning(=) null to the variable!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    ubh (09-25-2008)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by ubh View Post
    Why wont this work?
    Code:
    var getNextSibling = document.getElementById("someElement").nextSibling;
    	if (getNextSibling = null)
    	{
    	 alert("SORRY NO SIBLINGS");
    	}
    	else
    	{
    	alert(getNextSibling);
    	}
    It depends on what do you mean by nextSibling. An element Node (an HTML tag), a text Node, a comment Node...?

    Whenever you are searching for siblings you must specify the type of the element you are looking for (the nodeType)

    See also:
    http://developer.mozilla.org/en/DOM/element.nodeType

    You must also keep in mind that browsers count the childnodes (thus they refere the siblings) in different ways. For instance, Mozilla counts as childNodes (and siblings) all the possible text nodes (the empty sapces between tags, the "gaps"), while IE counts only some of them. This is another reason for a nodeType test is required, combined with a while loop

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function findNextSib(){
    var e=document.getElementById('myDiv'), nextS=e.nextSibling;
    while(nextS.nodeType!=3){
    nextS=nextS.nextSibling;
    }
    alert('the next text Node is: '+nextS.nodeValue);
    nextS=e.nextSibling;
    while(nextS.nodeType!=8){
    nextS=nextS.nextSibling;
    }
    alert('the next comment Node is: '+nextS.nodeValue);
    nextS=e.nextSibling;
    while(nextS.nodeType!=1){
    nextS=nextS.nextSibling;
    }
    alert('the next tag Node is: an element with the id='+nextS.id)
    }
    onload=findNextSib;
    </script>
    </head>
    <body>
    <div>
    	<div id="myDiv"></div>
    	nextSibling text
    	<!--nextSibling comment-->
    	<div id="nextSiblingTag"></div>
    </div>
    </body>
    </html>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    ubh (09-25-2008)


  •  

    Posting Permissions

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