Rilcon42
Rilcon42

Reputation: 9763

How to properly reference a checkbox value?

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

Answers (1)

Taplar
Taplar

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

Related Questions