views:

20

answers:

2

Hi everybody,

I try to get the first .myClass element of my page.

To do that, I use $(".myClass:eq(0)"); This is working perfectly but gives me the following console error:

"Warning: Pseudo-class or pseudo-element "eq" unknown." (Traduced from french)

Is that error normal? Is there a way to avoid it? Which syntax do you use to select first elements? I also tried $(".myClass:first"); but the problem is then the same with first.

Thank you in advance for your help David

+1  A: 

Your code is fine and correct. I'm wondering which jQuery version you are using and on what browser you are testing.

$(".myClass").first()

which is a shortcut for

$(".myClass").eq(0)

might solve your trouble. .first() will not use sizzle to query elements, but uses array slice to reduce the set of matched elements.

jAndy
That works perfectly with the first(). No warning anymore, thank you!
daviddarx
+1  A: 

you are getting this warning because eq() is not a valid css selector...

you are getting the same with :first because the :first pseudo-class is equivalent to :eq(0)..

and jQuery catches this warning, and then do the thing it needs to do with it...

just same with this :hidden

Reigel
Thank you for this explanation, I understand better now.
daviddarx