For some performance reasons, I am trying to find a way to select only sibling nodes of the selected node. For example,
<div id="outer">
<div id="inner1"> </div>
<div id="inner2"> </div>
<div id="inner3"> </div>
<div id="inner4"> </div>
</div>
If I selected inner1 node, is there a way for me to access its siblings, inner2-4
nodes?
There are a few ways to do it.
Either one of the following should do the trick.
FYI: The jQuery code-base is a great resource for observing Grade A Javascript.
Here is an excellant tool that reveals the jQuery code-base in a very streamlined way. http://james.padolsey.com/jquery/
have you checked the "Sibling" method in jQuery?
the n.nodeType == 1 check if the element is a html node and n!== exclude the current element.
I think you can use the same function, all that code seems to be vanilla javascript.
Use document.querySelectorAll() and Loops and iteration
Well... sure... just access the parent and then the children.
or... using jQuery:
Edit: Cletus as always is inspiring. I dug further. This is how jQuery gets siblings essentially: