Skip to content

Instantly share code, notes, and snippets.

@luboskmetko
Created April 20, 2021 10:55
Show Gist options
  • Save luboskmetko/d8f7f1129ed5cb73ca6ed39857c5e9ea to your computer and use it in GitHub Desktop.
Save luboskmetko/d8f7f1129ed5cb73ca6ed39857c5e9ea to your computer and use it in GitHub Desktop.
let el = document.querySelector('.nav');
el.className += 'is-open';
const children = el.querySelectorAll('li');
const textContents = children.map(child => child.textContent);
console.log(textContents); // should log an array of strings
@ndizeye-patrick
Copy link

document.addEventListener('DOMContentLoaded', function() {
let el = document.querySelector('.nav');

if (el) {
    el.classList.add('is-open'); // Adding a class properly
    const children = Array.from(el.querySelectorAll('li')); // Convert NodeList to array
    const textContents = children.map(child => child.textContent);
    
    console.log(textContents); // Log an array of strings
} else {
    console.error("Element with class 'nav' not found");
}

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment