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
2009-05-29 10:15:00
+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
2009-05-29 10:16:29
Non-element nodes will also be selected with that...
J-P
2009-05-29 10:29:40
...which Russ's method corrects for.
annakata
2009-05-29 10:39:02
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
2009-05-29 16:04:50