65

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?

Community
  • 1
  • 1
Earlz
  • 62,085
  • 98
  • 303
  • 499

6 Answers6

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

Note only in jQuery 1.6+

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
13
$('#checkbox').prop('checked', true);

When you want it unchecked:

$('#checkbox').prop('checked', false);
Jason Kaczmarsky
  • 1,666
  • 1
  • 17
  • 30
12
$('#test').attr('checked','checked');

$('#test').removeAttr('checked');
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
6

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);
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
5

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);
xkeshav
  • 53,360
  • 44
  • 177
  • 245
3

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;

N0ug4t
  • 191
  • 1
  • 12