Reputation: 9763
I am trying to determine the value of a checkbox. Why does the first JS example not work, but the second does?
CSHTML
<div style="border-style: solid;">
@Html.LabelFor(model => model.paios)
@Html.CheckBoxFor(model => model.paios)
</div>
JS
console.log($('#paios').checked); //UNDEFINED
$('#paios').change(function () {
console.log(this.checked); //returns true or false
}
Upvotes: 0
Views: 25
Reputation: 24965
Because checked
is an Element property, and jQuery does not directly expose Element properties. You have to use the accessory methods.
$('#paios').prop('checked')
//or break the Element out of the jQuery object
$('#paios')[0].checked
Upvotes: 1