-
-
Save luboskmetko/d8f7f1129ed5cb73ca6ed39857c5e9ea to your computer and use it in GitHub Desktop.
let el = document.querySelector('.nav');
el.classList.add('is-open');
const children = el.querySelectorAll('li');
const textContents = Array.from(children).map(child => child.textContent);
console.log(textContents); // Logs an array of text contents from
let el = document.querySelector('.nav');
el.classList.add('is-open');
const children = el.querySelectorAll('li');
const textContents = Array.from(children).map(child => child.textContent);
console.log(textContents);
let el = document.querySelector('.nav');
el.classList.add('is-open');
const children = el.querySelectorAll('li');
const textContents = Array.from(children).map(child => child.textContent);
console.log(textContents);
document.addEventListener('DOMContentLoaded', function() {
const el = document.querySelector('.nav');
if (el) {
el.classList.add('is-open');
const children = Array.from(el.querySelectorAll('li'));
const textContents = children.map(child => child.textContent);
console.log(textContents);
} else {
console.error("Element with class 'nav' not found");
}
});
- Class Name use - el.classList.add('is-open');
- NodeList and map():
querySelectorAll returns a NodeList, not a true Array. To use map, you must first convert the NodeList to an Array
let el = document.querySelector('.nav');
el.classList.add('is-open');
const children = el.querySelectorAll('li');
const textContents = Array.from(children).map(child => child.textContent);
console.log(textContents); // logs an array of strings
document.addEventListener('DOMContentLoaded', function() {
let el = document.querySelector('.nav');
});