eozzy
eozzy

Reputation: 68670

First element not with class

I want to select the first element that does not have a class .disabled.

I tried these without success:

$('.selected:first:not(".disabled")').addClass('first');

and

$('.selected:first').not(".disabled").addClass('first');

Upvotes: 0

Views: 87

Answers (4)

Ishan Jain
Ishan Jain

Reputation: 8171

Your code:

$('.selected:first:not(".disabled")').addClass('first');

this code selects the first element with selected class and than check is the the selected element not have disabled class.

Fiddle With Your code Output

Answer :-

First you need to select the elements which not have css class disabled.

Using:

$('.selected:not(.disabled)');

And after that select the first element into the matched element;

$('.selected:not(.disabled)').first();

Try this:

$('.selected:not(.disabled)').first().addClass('first');

Working Example

Upvotes: 1

wIRELESS
wIRELESS

Reputation: 196

Here you go

$('.selected').not('.disabled')[0]

Upvotes: 1

Alex Char
Alex Char

Reputation: 33218

$('.selected:not(.disabled):first').addClass('first');

Upvotes: 5

Dávid Szabó
Dávid Szabó

Reputation: 2247

You have to get all the element with class 'selected' but without class 'disabled' then get the first of them.

$('.selected:not(.disabled)').first().addClass('disabled');

or

$('.selected:not(.disabled):first').addClass('disabled');

Fiddle: http://jsfiddle.net/5LuXD/1/

Upvotes: 5

Related Questions