4

I want to check if a certain check box is selected using JavaScript/jQuery.

Here is the code I tried:

var a; if ($("#add_desc").checked == 1){ a = "true"; }else{ a= "false"; } 

I have also tried:

var a; if ($("#add_desc").checked){ a= "true"; }else{ a= "false"; } 

It always returns false once I alert the variable a.

Any idea why it won't work for me? Am I doing it wrong?

Thanks!

12
  • 1
    Add [0] before .checked and use your second attempt Commented Apr 20, 2012 at 21:04
  • By way of explanation: $('#add_desc') returns a jQuery object that contains the element, rather than the element itself, so doesn't have a checked property. Adding the [0] returns the first element contained in the jQuery object (your actual checkbox), which does have a checked property. Commented Apr 20, 2012 at 21:07
  • 1
    You can also access those properties with jQuery 1.6's .prop(). api.jquery.com/prop Commented Apr 20, 2012 at 21:08
  • @CameronSpear You can, though to be honest this entire example is trivial enough that there's no need to use jQuery at all. Commented Apr 20, 2012 at 21:09
  • @AnthonyGrist I've noticed you need to index jquery's id selector too. Why does it not say this in the jQuery manual? They do it like the OP is doing it.. Commented Apr 20, 2012 at 21:09

6 Answers 6

7

To get the value of the checked property on a checkbox input, use the .prop() method, or get the value directly from the DOM element. [0] returns the first selected DOM element.

var a; if ($("#add_desc")[0].checked){ a= "true"; }else{ a= "false"; } 

or

var a; if ($("#add_desc").prop("checked")){ a= "true"; }else{ a= "false"; } 

Edit
For versions of jQuery older than 1.6, .prop didn't exist, use .attr as a direct replacement.

Sign up to request clarification or add additional context in comments.

Comments

6

Simple,

var a = $('#element:checkbox').is(':checked'); 

2 Comments

That would make the variable a boolean while in his example he's assigning string values...
Yes. But how hard would it be to do a toString() if required?
6

The "jQuery way" would be to use .is(":checked"):

var a; if ($("#add_desc").is(":checked")){ // box is checked a = "true"; } else{ // box is not checked a= "false"; } 

I use this a lot in my web apps and it works perfectly.

From the jQuery .is documentation page:

Unlike other filtering methods, .is() does not create a new jQuery object. Instead, it allows you to test the contents of a jQuery object without modification. This is often useful inside callbacks, such as event handlers.

Comments

4

The ternary operator in javascript combined with jQuery .is() is the shortest solution.

var a = ($("#add_desc").is(":checked")) ? "true" : "false"; 

Comments

3

try:

if ($('#add_desc').is(':checked')) { a = "true"; } else { b = "false"; } 

you may also not want to use strings, i.e. use true and false instead of "true" and "false"

Comments

2

Try

$get("#add_desc").checked == true 

Also

$("#add_desc").prop("checked", true); $("#add_desc").prop("checked", false); 

1 Comment

Note that .prop() did not exist prior to jQuery 1.6. Use .attr() for any earlier versions of jQuery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.