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!
[0]before.checkedand use your second attempt$('#add_desc')returns a jQuery object that contains the element, rather than the element itself, so doesn't have acheckedproperty. Adding the[0]returns the first element contained in the jQuery object (your actual checkbox), which does have a checked property..prop(). api.jquery.com/prop