2

I'm working with jQuery and I have a bit of an issue. I have a bunch of DIVs with a certain class, the first of which I'm trying to select. I can't use the first-child pseudo class because of the page layout (long story). But I can get the same effect by treating the jQuery object as an array. My problem is that this does not return a jQuery object but rather a plain Javascript object (specifically a HTMLDivElement).

$("#wizard div.collapsible")[0].addClass("selected");

That fails because the object returned doesn't have an addClass function. It also fails if I use .get(0). However, this does work.

$($("#wizard div.collapsible")[0]).addClass("selected");

Is there a way to do this without have to do the double jQuery selector? (Not that it's a big deal, but it looks kind of confusing.)

1

5 Answers 5

9

This is what the eq and first functions do. eq obtains an element from a selection by a 0-based index, while first just gets the first element from a selection.

$("#wizard div.collapsible").eq(0).addClass("selected");
$("#wizard div.collapsible").first().addClass("selected");

Interestingly, though, you've actually hit on the fastest way to get a jQuery selection containing the first element from another selection:

$($("#wizard div.collapsible")[0]).addClass("selected");

It's not a great way to do it, because it's so hard to read and the performance benefits are minimal, but it is fastest!

Sign up to request clarification or add additional context in comments.

4 Comments

Not too worried about speed here, just readability. But that's interesting to say the least.
Also not mentioned is that [0] bracket notation will give you the plain javascript element, which strips the jQuery from it. Great if you don't need jQuery on it anymore!
Why does JQuery behave this way? It returns a JQuery object at some places, it does not at the other places... Is there any pattern?
@MinhNghĩa It returns a jQuery object unless you specifically ask for the native element(s).
2

[0] will be a raw element. That's correct and desired behaviour. If you want to retain the jQuery object, use $("selector:first") or $("selector").first() or $("selector").eq(0) to get the first element wrapped in a jQuery object.

4 Comments

A million answers, but I like the first pseudo class the best. Thank you.
@GJK The pseudo-class is the worst way to go, because it means jQuery has to do the heavy lifting. Browsers don't natively understand :first, so jQuery's selector engine has to do all the work.
Ahh, I wasn't aware that :first wasn't a native CSS selector. I'll probably go with the first() function then. Thank you.
@GJK It's in the pipeline for Selectors Level 4, but it's not here yet.
0

$.eq should do the trick:

$("#wizard div.collapsible").eq(0).addClass("selected");

If you select a class shared by more than one element, a jQuery collection will be returned. Attempting to access that collection as an array will return a raw object, whereas $.eq has been designed to return a jQuery object for your convenience.

Comments

0

Try..

$("#wizard div.collapsible").eq(0).addClass("selected");

Or... first()

$("#wizard div.collapsible").first().addClass("selected");

Comments

0

When you use $('#wizard div.collapsible')[0] you return a plain JS object as you said, since it's a plain JS object, and not a jQuery object, you're unable to use jQuery methods on it. You could either use the double selector or use $('#wizard div.collapsible')[0].className = "selected";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.