xxxxxxxxxx
// Method 1: Using querySelectorAll
const parentElement = document.querySelector('.parent'); // replace '.parent' with the appropriate parent element selector
const childElements = parentElement.querySelectorAll('.child'); // replace '.child' with the appropriate child element selector
console.log(childElements);
// Method 2: Using children property
const parentElement = document.querySelector('.parent'); // replace '.parent' with the appropriate parent element selector
const childElements = parentElement.children;
console.log(childElements);
// Method 3: Using getElementsByTagName
const parentElement = document.querySelector('.parent'); // replace '.parent' with the appropriate parent element selector
const childElements = parentElement.getElementsByTagName('div'); // replace 'div' with the appropriate child element tag name
console.log(childElements);
// Method 4: Using getElementsByClassName
const parentElement = document.querySelector('.parent'); // replace '.parent' with the appropriate parent element selector
const childElements = parentElement.getElementsByClassName('child'); // replace '.child' with the appropriate child element class name
console.log(childElements);
xxxxxxxxxx
let content = document.getElementById('menu');
let firstChild = content.firstChild.nodeName;
console.log(firstChild);Code language: JavaScript (javascript)
xxxxxxxxxx
// Get the parent element
const parentElement = document.getElementById("parent");
// Get all child elements of the parent using a CSS selector
const childElements = parentElement.querySelectorAll(".child");
// Iterate over the child elements
childElements.forEach((child) => {
// Perform operations on each child element
console.log(child);
});
xxxxxxxxxx
var child_element = document.querySelector("#id").children[0];
//Where # defines id and '.' a class. And 0 argument the first child element
xxxxxxxxxx
/*
<div id="parent">
<input value="off" />
<button type="button">Click Me</button>
</div>
*/
const input = document.querySelector('#parent input');
const button = document.querySelecctor('#parent button');
xxxxxxxxxx
let firstChild = parentElement.firstChild;
Code language: JavaScript (javascript)
xxxxxxxxxx
// Assuming you have an HTML element with the id 'parentElement' and you want to access its first child element
// Using the querySelector method to select the first child element of `parentElement`
const childElement = document.querySelector('#parentElement > :first-child');
// Accessing the child element's properties or manipulating it
console.log(childElement); // Output: The first child element of parentElement
xxxxxxxxxx
// given html
<div class="parent">
<div class="child1">
<div class="child2">
</div>
</div>
</div>
// Get parent
let parent = document.querySelector('.parent');
// child element selector
parent.querySelector('.child')
xxxxxxxxxx
let lastChild = parentElement.lastElementChild;Code language: JavaScript (javascript)