Find Next Sibling Element in a DOM with JavaScript

How can I find the next item in the current area? For example, I want to find an element div, then the next p?

If I use document.getElementsByTagName('DIV')[0].nextSibling, it returns #text. I want it to return a tag p.

<div>
    <table>
        <tr>
            <td></td>
        </tr>
    </table>
</div>
<p></p>
+4
source share
2 answers

You can use . If you need to support older browsers, navigate until you find the node element. nextElementSiblingnextSibling

+8
source

You want to .nextElementSibling.

document.getElementsByTagName('DIV')[0].nextElementSibling

, #text, , nextSibling node childNodes . (, #text) - , </div> <p>.

+3

Source: https://habr.com/ru/post/1570205/


All Articles