Answers for "nextsibling vs nextelementsibling"

0

nextsibling vs nextelementsibling

// nextElementSibling always returns an element. 
// nextSibling can return any kind of node.
// They are the same for your example, but different in other
<p>
  <span id="span-01">Here is span-01</span>
  Some text at the top level
  <span id="span-02">Here is span-02</span>
</p>
document.getElementById('span-01').nextElementSibling //is  span-02
document.getElementById('span-01').nextSibling // is the text node containing 
											  //  "Some text at the top level" 
//note:
//
<div id="div-1">Here is div-1</div> 
<div id="div-2">Here is div-2</div>
var el = document.getElementById('div-1').nextSibling // is #text node
var el2 = document.getElementById('div-2').nextSibling // is #text node
//text nodes are inserted in the DOM where whitespace occurs between tags
//(i.e. after the closing tag of an element and before the opening tag of the next).
//source : https://developer.mozilla.org/en-US/docs/Web/API/Node/nextSibling#example
Posted by: Guest on September-11-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language