Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is jQuery an Array?

Tags:

arrays

jquery

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
}
like image 964
enamrik Avatar asked Apr 29 '11 02:04

enamrik


1 Answers

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.

like image 102
JAAulde Avatar answered Oct 16 '22 15:10

JAAulde