Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a checkbox checked with jQuery? [duplicate]

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?

like image 338
Earlz Avatar asked Jul 27 '11 20:07

Earlz


4 Answers

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

Note only in jQuery 1.6+

like image 132
Dunhamzzz Avatar answered Nov 17 '22 02:11

Dunhamzzz


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

$('#test').removeAttr('checked');
like image 39
ShankarSangoli Avatar answered Nov 17 '22 01:11

ShankarSangoli


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

When you want it unchecked:

$('#checkbox').prop('checked', false);
like image 13
Jason Kaczmarsky Avatar answered Nov 17 '22 03:11

Jason Kaczmarsky


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);
like image 6
Nicola Peluchetti Avatar answered Nov 17 '22 02:11

Nicola Peluchetti