Check a checkbox using jQuery or JavaScript?

To check this you can Use the new .prop() function:

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

But it will work only injQuery 1.6 and above. 
To check in jQuery 1.5 and below versions you need to use .attr().

To check the checkbox (by setting the value of the checked attribute) do

and for un-checking (by removing the attribute entirely) do


If you're working with just one element, it will always be fastest to use DOMElement.checked = true. 
The benefit to using the .prop() and .attr() functions is that they will operate on all matched elements.

// Assuming an event handler on a checkbox
if (this.checked)

$('.chkTest').each(function(){ this.checked = true; });

$('.chkTest').each(function(){ this.checked = false; });

By doing this, you are using JavaScript standards for checking and unchecking checkboxes, so any browser that properly implements the "checked" property of the checkbox element will run this code flawlessly. This should be all major browsers

This selects elements that have the specified attribute with a value containing the given substring:

$('input[name *= ckbItem]').attr('checked', true);

It will select all elements that contain ckbItem in its name attribute.

If you know the value

$("input.myclass[name='myname'][value='the_value']").prop("checked", true);


Post a Comment

Online Casino