Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do jQuery selectors return an array of HTML Element or jQuery Objects?

Tags:

jquery

What is stored in Q?

Q = $('div');
Q2 = document.getElementsByTagName('div');

I can access each HTML element by using Q[index], similar to Q2[index]; which makes it seem like Q is an array of HTML Elements.

On the other hand, I can do Q.filter(), but I cannot do Q2.filter(); which makes it seem like Q is an array of jQuery objects.

Or is it both, where Q is a jQuery object that holds a bunch of HTML elements? If this was the case, wouldn't console.log() detect Q as an object with a collection of objects inside it? This fiddle, http://jsfiddle.net/rkw79/3s7tw/, shows that they are the same.

Note: I am aware that Q.eq(index) will return an object that can use jQuery methods. I'm just wondering what exactly is Q

like image 317
rkw Avatar asked Aug 09 '11 09:08

rkw


1 Answers

The result is a jQuery object that behaves like both an array of HTMLElements which you get using [] and an array of jQuery objects which you get using eq(index);

like image 133
Mircea Nistor Avatar answered Nov 04 '22 13:11

Mircea Nistor