I'm just getting into jQuery and I am having problems understanding what it is. How can I use array style indexing on a jQuery object but jQuery not be an array? Is this a javascript thing?
<ul id="myUL">
<li></li>
<li id="second"></li>
<li></li>
</ul>
var jqueryObject = $("#myUL > li");
alert(jqueryObject[1].attributes.getNamedItem("id").value);
if (jqueryObject instanceof Array) {
alert('value is Array!');
} else {
alert('Not an array');//this is what pops up
}
A jQuery collection is an Object with properties numbered like Array indexes (and some other properties and methods), each holding one of the matched elements. It is also given a length property to tell you how many elements the selector matched. See http://api.jquery.com/Types/#jQuery
Also, yes, it is partly a JavaScript thing--JS lets you access an object's properties with dot notation or with square-bracket notation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With