Answers for "Parents, Children & Siblings"

0

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
Posted by: Guest on April-18-2022

Code answers related to "Javascript"

Browse Popular Code Answers by Language