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
You want .nextElementSibling.
document.getElementsByTagName('DIV')[0].nextElementSibling
The reason you are getting #text
is because nextSibling
will return the next node in the childNodes
list of the parent element. In this case it is whitespace (hence #text) because of the newline used between </div>
and <p>
.
You can use nextElementSibling. If you need to support older browsers, traverse nextSibling
until you find an Element node.