How to make a checkbox checked with jQuery?

JqueryHtmlCheckbox

Jquery Problem Overview


> Possible Duplicate:
> How do I check a checkbox with JQuery or Javascript?

I'm trying to make a checkbox checked (or not) with jQuery.

My example HTML:

<input type="checkbox" id="test" name="test" />

Attempt at clearing a checkbox(doesn't work)

$('#test').val('off');

and at checking:

$('#test').val('on');

How do I control checkboxes with jQuery?

Jquery Solutions


Solution 1 - Jquery

$('#test').prop('checked', true);

Note only in jQuery 1.6+

Solution 2 - Jquery

$('#checkbox').prop('checked', true);

When you want it unchecked:

$('#checkbox').prop('checked', false);

Solution 3 - Jquery

$('#test').attr('checked','checked');

$('#test').removeAttr('checked');

Solution 4 - Jquery

I think you should use prop(), if you are using jQuery 1.6 onwards.

To check it you should do:

$('#test').prop('checked', true);

to uncheck it:

$('#test').prop('checked', false);

Solution 5 - Jquery

from jQuery v1.6 use prop

to check that is checkd or not

$('input:radio').prop('checked') // will return true or false

and to make it checkd use

$("input").prop("checked", true);

Solution 6 - Jquery

You don't need to control your checkBoxes with jQuery. You can do it with some simple JavaScript.

This JS snippet should work fine:

document.TheFormHere.test.Value = true;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEarlzView Question on Stackoverflow
Solution 1 - JqueryDunhamzzzView Answer on Stackoverflow
Solution 2 - JqueryJason KaczmarskyView Answer on Stackoverflow
Solution 3 - JqueryShankarSangoliView Answer on Stackoverflow
Solution 4 - JqueryNicola PeluchettiView Answer on Stackoverflow
Solution 5 - JquerydiEchoView Answer on Stackoverflow
Solution 6 - JqueryN0ug4tView Answer on Stackoverflow