SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: getNextElement

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    sydney
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getNextElement

    I'm trying to understand how this function works. I know that it allows you to get the nextSibling element in a document but how? Anyone can explain what the 2 if statements are doing would be a great help.

    function getNextElement(node) {
    if(node.nodeType == 1) {
    return node;
    }
    if (node.nextSibling) {
    return getNextElement(node.nextSibling);
    }
    return null;
    }

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does not get the next element if you pass it an element- it returns the same element.


    This example is simpler, in that it doesn't recurse, and it does more-
    it returns the next or the previous sibling element, whatever node type you start with.

    Code:
    function findSib(who,previous){	
    	var X= (previous)? 'previousSibling': 'nextSibling';	
    	var sib= who[X];
    	while(sib){
    		if(sib.nodeType==1) return sib;
    		sib= sib[X];
    	}
    	return false;
    }


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
  •