Select All checkboxes using jQuery

Jak
Jak
908 Points
132 Posts

hi,

I have following html code

<input type="checkbox" id="chkCheckAll" />
<p id="checkBoxes">
   <input type="checkbox" class="myBoxClass" id="Checkbox1" />
   <br />
   <input type="checkbox" class="myBoxClass" id="Checkbox2" />
   <br />
   <input type="checkbox" class="myBoxClass" id="Checkbox3" />
   <br />
   <input type="checkbox" class="myBoxClass" id="Checkbox4" />
   <br />
   <input type="checkbox" class="myBoxClass" id="Checkbox5" />
   <br />
</p>

Whenever user checks chkCheckAll all checkboxes in <p>...</p> must be checked. Also I have following jquery code:

 $(document).ready(function () {
        $("#chkCheckAll").click(function () {
            $(".myBoxClass").attr('checked', this.checked);
        });
    });

I am getting following result: In the first click on chkCheckAll all checkboxes were checked(which is correct). In the second click on chkCheckAll all checkboxes were unchecked (which is correct). But in 3th attempt nothing happened! also in 4th attempt nothing happened and so on.

Views: 8754
Total Answered: 2
Total Marked As Answer: 0
Posted On: 25-Mar-2015 03:07

Share:   fb twitter linkedin
Use $(".myBoxClass").prop() instead of $(".myBoxClass").attr()
 - Brian  25-Oct-2017 06:35
Answers
beginer
beginer
1576 Points
53 Posts
         

Use following code:

$(document).ready(function () {
    $(".class").on('click', function () {
        $(".checkbox).prop('checked', true);
    });
});
Posted On: 25-Oct-2017 06:38
Smith
Smith
2914 Points
78 Posts
         

Try to use following

$(document).ready(function () {
        $("#chkCheckAll").click(function () {
            $(".myBoxClass").prop('checked', this.checked);
        });
    });
Posted On: 25-Oct-2017 06:40
 Log In to Chat