0

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.

justnajm
  • 4,422
  • 6
  • 36
  • 56

2 Answers2

3

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);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Isn't this possible for jQuery to fallback to DOMElement property if property is requested, just curious – justnajm Jul 13 '16 at 16:03
  • Possible, yes. But it would be a massive breaking change, and possibly lead to a lot of confusion. By specifically having to use `prop('propName')` or `get(0).propName` your intent of getting the property value is explicit. – Rory McCrossan Jul 13 '16 at 16:05
0

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

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177