How can I find the next element in the current scope? For example, I want to find div
element and after this the next one which is p
?
If I use document.getElementsByTagName('DIV')[0].nextSibling
it returns #text
. I want it to return p
tag.
<div>
<table>
<tr>
<td></td>
</tr>
</table>
</div>
<p></p>
The Element. nextElementSibling read-only property returns the element immediately following the specified one in its parent's children list, or null if the specified element is the last one in the list.
To get the previous sibling of a specific HTML Element, using JavaScript, get reference to this HTML element, and read the previousElementSibling property of this HTML Element. previousElementSibling property returns the element immediately prior to this HTML element.
You can use nextElementSibling
. If you need to support older browsers, traverse nextSibling
until you find an Element node.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With