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
@PATRICKNDIZEYE
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");
}

});

@SadneyBim
Copy link

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

  • elements

  • @danish93develop
    Copy link

    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);

    @hamzahanif192
    Copy link

    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);

    @Mukeshoad
    Copy link

    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");
    }
    });

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