justnajm
justnajm

Reputation: 4534

Why jQuery throwing undefined for checkbox checked property ?

Following code works inside the onchange property of checkbox:

 onchange="if(this.checked)$('#row_date').show();else $('#row_date').hide();" 

but jquery

alert($('#apply_range').checked) // throw undefined, either checked or unchecked

not working.

Upvotes: 0

Views: 310

Answers (2)

Michał Perłakowski
Michał Perłakowski

Reputation: 92531

You have to either use $('#apply_range').is(":checked") or directly access DOM element: $('#apply_range')[0].checked.

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337580

checked is a property of the DOMElement, not a jQuery object. To get that property from the jQuery object, use prop():

console.log($('#apply_range').prop('checked'));

Or you can access the DOMElement in the jQuery object directly like this:

console.log($('#apply_range')[0].checked);

// or

console.log($('#apply_range').get(0).checked);

Upvotes: 3

Related Questions