I would like to do something similar to mark a checkbox using jQuery :

$(".myCheckBox").checked(true); 

or

 $(".myCheckBox").selected(true); 

Is it possible

Translation of the question “ Setting“ checked ”for a checkbox with jQuery? » @Tpower .

1 answer 1

jQuery 1.6+

Use the new .prop() function:

 $('.myCheckbox').prop('checked', true); $('.myCheckbox').prop('checked', false); 

jQuery 1.5.x and below

The .prop() function is not available, so use .attr() .

 $('.myCheckbox').attr('checked', true); $('.myCheckbox').attr('checked', false); 

Note that this is the approach used in jQuery unit testing up to version 1.6 , more preferred than

 $('.myCheckbox').removeAttr('checked'); 

Since the latter, when initially marked with a checkbox, changes the behavior of a call to .reset() to any form containing it - the change, though not so noticeable, but undesirable.

You can get into context by reading the unfinished discussion about changes in attribute processing / property when moving from version 1.5.x to 1.6, which is in the information for version 1.6 and in the section Attributes vs. Properties .prop() -documentation .

Any jQuery version

If you work with just one element, you can always simply change the HTMLInputElement 's .checked :

 $('.myCheckbox')[0].checked = true; $('.myCheckbox')[0].checked = false; 

The advantage of using the .prop() and .attr() functions instead is that they will work with all the elements that match the specified conditions.

Translation of the answer “ Setting“ checked ”for a checkbox with jQuery? » @Xian .