R
R
Rem362021-06-23 12:44:47
JavaScript
Rem36, 2021-06-23 12:44:47

How to apply class?

I'm so sorry, I did that.

window.addEventListener('DOMContentLoaded', function(){
    document.querySelector("#input-checkbox").addEventListener('change', function(){
        document.querySelector('#color-label-editions').classList.toggle('active-label-color')
    })
});


I have a lot of checkboxes and for each I'm guessing doing id is not the right decision, maybe I'm wrong.

I tried to do it through a class, but for some reason it doesn't work. What's wrong?

window.addEventListener('DOMContentLoaded', function(){
    document.querySelectorAll("div.input-checkbox").addEventListener('change', function(){
        document.querySelectorAll('div.color-label-editions').classList.toggle('active-label-color')
    })
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Simkav, 2021-06-23
@Simkav

querySelector returns a single element in this case everything will work, but querySelectorAll returns a NodeList collection to which you are trying to add a listener and a class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question