How to get not a neighboring element, but through it in JS?

  • 0
    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 a p if such an analogue is in pure JavaScript? allowing you to walk inside the parent? by property type previousElementSibling nextElementSibling , but accessed through the element
    JavaScript Anonymous, Jul 28, 2019

  • 3 Answers
  • 0
    <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

  • 0
    There are no such methods. Take next, then another next, etc.
    Anonymous

  • 0
    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

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!