how to get child element in javascript code example

Example 1: get child of child javascript

var li = document.querySelector("li");
var number = 13;
li.children[0].children[0].innerHTML = number;

Example 2: how to get child element in javascript

let content = document.getElementById('menu');
let firstChild = content.firstChild.nodeName;
console.log(firstChild);Code language: JavaScript (javascript)

Example 3: select child javascript

Syntax
element.children

Example
Change the background color of the second child element of a <div> element:

var c = document.getElementById("myDIV").children;
c[1].style.backgroundColor = "yellow";

Example 4: how to get child element in javascript

let firstChild = parentElement.firstChild; 
Code language: JavaScript (javascript)

Example 5: how to get child element in javascript

let lastChild = parentElement.lastElementChild;Code language: JavaScript (javascript)

Tags:

Misc Example