A selector will always return a jQuery object. Which is an array of elements that matched the selector - with some additional functionality.

So, $("#myElement") returns a jQuery object. Which allows us to do things like $("#myElement").hide();

But sometimes you need the original DOM element reference. This is part of the returned jQuery object - it is the element in the array. So, if $("#myElement") matches only one item, you can do $("#myElement")[0].className;

It is a little trickier with selectors that return multiple elements, but no more so than dealing with a regular array. $(".myClass")[3].className = "theClass"; - this would set the class of the 4th matched element to "theClass".

Of course, coding being what it is there are more than one way to do things. That last sample could also be written $(".myClass:eq(3)").addClass("theClass"); to keep it with the jQuery concepts.

Knowing the DOM structure, Javascript fundamentals, AND jQuery is a great combination when you get those "oddball" situations.

HTH.

Shawn

Wizzud wrote:
The Selector will always return an array of zero, one, or more
elements.

I'm not sure why the distinction has been made between Element and
Array of Elements - the only reason I could come up with was that
someone wanted to indicate that certain Selectors will/should return
only one element, albeit it still in an array.

On Apr 4, 8:35 pm, deer421 <[EMAIL PROTECTED]> wrote:
I am a beginner in jQuery. On the API doc (http://docs.jquery.com/
Selectors), it says some selectors return Element and other return
Array <Element>. To me they all return Array <Element>. Even #id
returns an array of one DOM Element. Am I correct? confused?

Thanks.

Reply via email to