tags:

views:

265

answers:

3

There is a native "nextSibling" attribute,but how to get "lastSibling"?

+4  A: 

A Few functions to handle DOM events

from the article

function lastSibling(node){
    var tempObj=node.parentNode.lastChild; 
    while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){  
    tempObj=tempObj.previousSibling;   
    }  
    return (tempObj.nodeType==1)?tempObj:false; 
}
Russ Cam
+1  A: 

There's no direct method to access the last sibling; you simply need to iterate over all of the siblings until you reach the last one.

Andrzej Doyle
A: 

Maybe:

Node.parentNode.lastChild

Hope this helps!

Sergey Ilinsky
Non-element nodes will also be selected with that...
J-P
...which Russ's method corrects for.
annakata
The question doesn't have any concerns related to that. Also, since author expressed awareness of nextSibling property, I thought he was aware of implications regarding document traversal and presence of different kinds of nodes in there.
Sergey Ilinsky