Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  172] [ 2]  / answers: 1 / hits: 146291  / 12 Years ago, fri, february 8, 2013, 12:00:00

I have the following html code:



    <input type=checkbox id=ckbCheckAll />
<p id=checkBoxes>
<input type=checkbox class=checkBoxClass id=Checkbox1 />
<br />
<input type=checkbox class=checkBoxClass id=Checkbox2 />
<br />
<input type=checkbox class=checkBoxClass id=Checkbox3 />
<br />
<input type=checkbox class=checkBoxClass id=Checkbox4 />
<br />
<input type=checkbox class=checkBoxClass id=Checkbox5 />
<br />
</p>


When user checks ckbCheckAll all checkboxes must be checked. Also I have following jquery code:



    $(document).ready(function () {
$(#ckbCheckAll).click(function () {
$(.checkBoxClass).attr('checked', this.checked);
});
});


When I see my page in the browser I get the following result:
In the first click on ckbCheckAll all checkboxes were checked (which is correct). In the second click on ckbCheckAll all checkboxes were unchecked (which is correct). But in 3rd attempt nothing happened! also in 4th attempt nothing happened and so on.



Where is the problem?


More From » jquery

 Answers
19

Use prop



$(.checkBoxClass).prop('checked', true);


or to uncheck:



$(.checkBoxClass).prop('checked', false);


http://jsfiddle.net/sVQwA/



$(#ckbCheckAll).click(function () {
$(.checkBoxClass).prop('checked', $(this).prop('checked'));
});


Updated JSFiddle Link: http://jsfiddle.net/sVQwA/1/


[#80339] Thursday, February 7, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckinley

Total Points: 15
Total Questions: 101
Total Answers: 94

Location: Liechtenstein
Member since Fri, Sep 11, 2020
4 Years ago
;