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.

3
  • alert($('#apply_range').get(0).checked) Commented Jul 13, 2016 at 9:39
  • 1
    Possible duplicate of Check if checkbox is checked with jQuery Commented Jul 13, 2016 at 9:42
  • I recommend you don't use inline JS event attributes. Instead use an event listener or jQuery's on method. Commented Jul 13, 2016 at 9:46

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't this possible for jQuery to fallback to DOMElement property if property is requested, just curious
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.