I'm trying to find a way to select a child node of a sibling node with pure Javascript. Any help would be appreciated.
Asked
Active
Viewed 209 times
0
-
Select from where? Do you mean in a selector string? `".foo + .bar > .baz"` Or do you mean from an element you have a reference to? `my_foo.nextElementSibling.children[0]` Please be more specific. Is it something other than the very next sibling? There are too many ways to interpret this. – Jun 29 '15 at 20:58
-
This question shows how to get all the siblings: http://stackoverflow.com/questions/842336/is-there-a-way-to-select-sibling-nodes/22799075#22799075. Getting the children should be an easy addition. – Barmar Jun 29 '15 at 20:59
-
yeah from an element I have a reference to. – Brandon Jun 29 '15 at 21:02
1 Answers
0
you can use querySelector
. But you to take care of browser version
.
alert(document.querySelector('#sibling1 ~ #sibling2').children[0].innerHTML); // give desired child index
<div id="sibling1">sibling1</div>
<div id="sibling2">
<div id="sibling2-child">sibling2-child-1</div>
<div id="sibling3-child">sibling2-child-2</div>
</div>

ozil
- 6,930
- 9
- 33
- 56