R
R
Rem362021-06-23 10:35:12
JavaScript
Rem36, 2021-06-23 10:35:12

Why doesn't the class switch?

Hello. I'm trying to switch the class of the label on click, but it doesn't switch. What is the problem here?

<label class="label-checkbox-editions" id="color-label-editions">
                            <input type="checkbox" name="" class="section__editions-checkbox">
                            Бестселлеры
                            <span></span>
                        </label>


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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-06-23
@Rem36

Switches. Twice. Google what happens when a label is clicked. Also google "event bubbling".
Instead of clicking on the label, you should track the change on the input.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question