2

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"                       
            });         
        }
    });
2
  • 1
    Any errors in the console? Did you include jquery properly? It works: jsfiddle.net/4ydn8cuj Commented Jan 12, 2016 at 4:16
  • 2
    This could be as simple as $('#edit-field-industry-und-45').click(function() { //on click $('.checkall').prop('checked', this.checked); }); Commented Jan 12, 2016 at 4:20

2 Answers 2

2
$('#edit-field-industry-und-45').change(function () {
        $('input[type="checkbox"]').each(function () {
            $(this).prop("checked", true);
        });
    })
Sign up to request clarification or add additional context in comments.

Comments

1

Check out my DEMO . I am sure you will like it

<input type="checkbox" id="anchor-from"/>main

<input type="checkbox" class="checkall" id="period-daily" disabled />CheckBox2
<input type="checkbox" class="checkall" id="period-weekly"disabled />CheckBox3
<input type="checkbox" class="checkall" id="period-weekly2"disabled />CheckBox3
<input type="checkbox" class="checkall" id="period-weekly3"disabled />CheckBox3
<input type="checkbox" class="checkall" id="period-weekly4"disabled />CheckBox3




$("#anchor-from").change(function(){
                if($('#anchor-from').is(':checked'))
                {
                  $(".checkall").attr("disabled", false); 
                  $(".checkall").prop("checked", true); 

                }
                else
                {
                $(".checkall").attr("disabled", true);
                 $(".checkall").prop("checked", false); 
                }



            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.