Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Parents, Children & Siblings

let mainDiv = document.getElementById('main-div');  // First get the div itself

let parent = mainDiv.parentNode;  // the div's parent element
let children = mainDiv.children;  // the div's children
let firstChild = mainDiv.children[0];  // the div's first child
let nextSibling = mainDiv.nextElementSibling;  // the next element at the same nesting level
let prevSibling = mainDiv.previousElementSibling;  // the previous element at the same nesting level
 
PREVIOUS NEXT
Tagged: #Children #Siblings
ADD COMMENT
Topic
Name
5+7 =