lundi 11 janvier 2016

Select all checkbox values by selecting the first checkbox in an array with JQuery

I am trying to check all checkboxes if a user clicks the first checkbox.

HTML

 <input class="checkall" type="checkbox" id="edit-field-industry-und-45" name="field_industry[und][45]" value="45"> 
 <input class="checkall" type="checkbox" id="edit-field-industry-und-24" name="field_industry[und][24]" value="24"> 
 <input class="checkall" type="checkbox" id="edit-field-industry-und-25" name="field_industry[und][25]" value="25">
 <input class="checkall" type="checkbox" id="edit-field-industry-und-26" name="field_industry[und][26]" value="26">
 <input class="checkall" type="checkbox" id="edit-field-industry-und-27" name="field_industry[und][27]" value="27">

Here is what I tried

$('#edit-field-industry-und-45').click(function(event) {  //on click 
        if(this.checked) { // check select status
            $('.checkall').each(function() { //loop through each checkbox
                this.checked = true;  //select all checkboxes with class "checkbox1"               
            });
        }else{
            $('.checkall').each(function() { //loop through each checkbox
                this.checked = false; //deselect all checkboxes with class "checkbox1"                       
            });         
        }
    });




Aucun commentaire:

Enregistrer un commentaire