Select the first item that does not have a specific class.

I want to write a selector that targets the first element that does not 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 do not have "someClass":

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

But I do not know how to select only the first element that does not have "someClass".

Does anyone know how to do this?

+3
source share
5 answers
$('li:not(.someClass):first')

Here is a working demo: http://jsbin.com/arosa

+5
source

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

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

but as indicated probably

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

anyone eq(0)will select the first item

+1
source

, :not():

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

:

alert($('li:not(li.someClass)').eq(0).html());
+1

$('li first-child not:(.someClass)').dostuff()
0

Source: https://habr.com/ru/post/1725524/


All Articles