views:

67

answers:

5

I want to write a selector that targets the first element that doesn't have a specific class.

<ul>
<li class="someClass">item 1</li>
<li>item 2</li> <-- I want to select this
<li class="someClass">item 3</li>
<li>item 4</li>
</ul>

I know how to select all elements that doesn't have "someClass":

$('li not:(.someClass)').dostuff();

But I don't know how to only select the first element that doesn't have "someClass".

Anyone knows how to do this?

A: 

try

$('li first-child not:(.someClass)').dostuff()
sanders
I *think* this would target the first child IF it doesn't have someClass rather than the first child that doesn't have some class...but I've not tested it (or my solution either!)
Andiih
+1  A: 

see http://docs.jquery.com/Core/eq

$('li not:(.someClass)').eq(0).dostuff();

but as pointed out its probably

$('li:not(.someClass)').eq(0).dostuff();

whatever eq(0) will select the first element

Andiih
+1  A: 

You just have a typo, the syntax is :not():

$('li:not(.someClass)').eq(0)
Gumbo
+1  A: 

Try this :

alert($('li:not(li.someClass)').eq(0).html());
Canavar
+5  A: 
$('li:not(.someClass):first')

Here's a working demo: http://jsbin.com/arosa

Kobi
I think this is a nicer solution than using the eq(0) filter - probably more efficient
Andiih