A
A
aviatoru2020-04-25 09:28:35
JavaScript
aviatoru, 2020-04-25 09:28:35

How to add class to all ul inside div on click?

Help rewrite the script. Now, on click, an additional class is added and removed to the element. I need to do the same but add a class to all uls on the page. Thanks

const btnn = document.querySelector('#btnn');
    btnn.addEventListener('click', () => {
      [...document.querySelectorAll('.right-answer')]
        .forEach(el => el.classList.toggle('show'));
        ul.classList.toggle('show');
        btnn.classList.toggle('show');
    });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2020-04-25
@aviatoru

Isn't that the whole code? What is ul?
However, you need to select all the uls on the page AND add a show class to each element . With your example, that's probably the case. But, what is ul.classList.toggle('show');
document.querySelectorAll('ul')
.forEach(el => el.classList.toggle('show'));

const btnn = document.querySelector('#btnn');
    btnn.addEventListener('click', () => {
      [...document.querySelectorAll('ul')]
        .forEach(el => el.classList.toggle('show'));
        ul.classList.toggle('show');
        btnn.classList.toggle('show');
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question