Kye
Kye

Reputation: 6239

JQuery not using first found item in selector

I'm trying to figure how why JQuery is not using the first item in my selector.

<input type="checkbox" id="foo1" value="Hello World">

This is what I expect should work, but returns "undefinied"

$("#foo1").checked

Strangely, this works correctly...

$("#foo1")[0].checked

Am I missing something? I'm using JQuery 1.9.1 and Chrome.

Upvotes: 2

Views: 43

Answers (1)

Phil
Phil

Reputation: 164739

The jQuery function $( selector ) does not return an element but a jQuery object which is kind of like an array. One thing it does not have is a checked property.

If you want to get the checked property without resorting to referencing an element by index (as in your second example), you can use

$('#foo1').prop('checked')

See http://api.jquery.com/prop/

Get the value of a property for the first element in the set of matched elements

Upvotes: 3

Related Questions