Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to toggleclass for each label checkboxes in jquery?

I want to toggle class for the below code but not working. I want to add the class "checkbox-active" to each label when clicked and remove that class if clicked again.

<div class="sc-97ae821b-0 kgLRyG">
    <label id="selector-wrapper" class="sc-dc4336f8-0 jEDHBt sc-b5b652d3-0 eRKHhZ sc-97ae821b-1 jqCZMD problem-label" tabindex="0">
    <input name="problemAreas" type="checkbox" class="sc-dc4336f8-1 hvrnmr" value="CHEST">

    <label id="selector-wrapper" class="sc-dc4336f8-0 jEDHBt sc-b5b652d3-0 eRKHhZ sc-97ae821b-1 jqCZMD problem-label" tabindex="0">
    <input name="problemAreas" type="checkbox" class="sc-dc4336f8-1 hvrnmr" value="HAND">
</div>

What I have tried:

$(function() {
   $('.sc-dc4336f8-0').click(function() { // when a .myDiv is clicked
     
     $(this).addClass('checkbox-active');

     if($(this).hasClass('checkbox-active')){
           $(this).removeClass('checkbox-active').addClass('checkbox-active');
           
       }
   })
 })

The above code is working but what I want is that I want to remove the class "checkbox-active" also onclick if that label already has the class "checkbox-active"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try below script. I have added "on()" function on input class.

$('.sc-dc4336f8-1').on('change', function() {
                        $(this).toggleClass('checkbox-active')});
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading