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
    – Kevin B
    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
    – CWSpear
    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.

6

Simple,

var a = $('#element:checkbox').is(':checked');
2
  • That would make the variable a boolean while in his example he's assigning string values...
    – Mike B
    Commented Aug 6, 2012 at 22:53
  • Yes. But how hard would it be to do a toString() if required? Commented Aug 7, 2012 at 5:25
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.

4

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

var a = ($("#add_desc").is(":checked")) ? "true" : "false";
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"

2

Try

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

Also

$("#add_desc").prop("checked", true);
$("#add_desc").prop("checked", false);
1
  • Note that .prop() did not exist prior to jQuery 1.6. Use .attr() for any earlier versions of jQuery.
    – Nathan
    Commented Apr 20, 2012 at 21:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.