vendredi 14 mai 2021

Adding +1 in the end of the class

I have been studying JavaScript/JQuery lately on my free time. I am trying to make script choose between 2 checkboxes. If first checkbox is true then the second is false, if first one is false then second is true. This one works but it doesn't work on multiple classes. I have added 2 classes .checkbox-group1 and .checkbox-group2. I tried to make script add +1 to .checkbox-group 2 times but it only adds so I would get classes .checkbox-group1 and .checkbox-group2 but I only get .checkbox-group1.

Javascript/JQuery

$(document).ready(function(){
    
    var i = 0;

if(i<2){

i++;

}

else{

i = 1;

}
    
    $('.checkbox-group'+ i +' input:checkbox').click(function() {
    $('.checkbox-group'+ i +' input:checkbox').not(this).prop('checked', false);
    });
});

HTML CHECKBOXES

<div class="checkbox-group1 required">
Yes: <input type="checkbox">
No: <input type="checkbox">
</div>


<div class="checkbox-group2 required">
yes: <input type="checkbox">
No: <input type="checkbox" >
</div>



Aucun commentaire:

Enregistrer un commentaire