How to get not a neighboring element, but through it in JS?
-
How to get in pure JavaScript not the previous or adjacent element, but for example in css there is a tilde icon
~
which allows you to walk through the elements inside the parent, even if there is ap
if such an analogue is in pure JavaScript? allowing you to walk inside the parent? by property type previousElementSibling nextElementSibling , but accessed through the elementJavaScript Anonymous, Jul 28, 2019 -
<ul class="menu">
<li class="first">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li class="last">5</li>
</ul>
const menu = document.querySelector('.menu')
console.log(menu.firstElementChild)
console.log(menu.children[2])
console.log(menu.lastElementChild)
console.log(menu.children[menu.children.length-1])Joseph Lin -
There are no such methods. Take next, then another next, etc.Anonymous
-
https://developer.mozilla.org/en/docs/Web/API / Pare ... if all elements inside the parent are needed. You can filter them and get what you need.
Or call
element.nextElementSibling.nextElementSibling // Соседний через один
Anonymous
3 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!