peter
peter

Reputation: 2103

Exclude one element in .each

How to run through all textareas, excluding one whose id I have?

$("textarea").each(function (i, v) {
    // i could use an "if" inside here, but would rather adjust the selector
});

I have tried using :not($('#mySpecialTextarea')), but could not get that to work.

Upvotes: 1

Views: 230

Answers (1)

Manwal
Manwal

Reputation: 23816

Use jquery .not()

$("textarea").not('#mySpecialTextarea').each(function (i, v) {
    // your code
});

DEMO

You can also use :not()

$("textarea:not('#mySpecialTextarea')").each(function (i, v) {
    // your code
});

DEMO

The .not() method will end up providing you with more readable selections than pushing complex selectors or variables into a :not() selector filter. In most cases, .not() is a better choice.

Upvotes: 4

Related Questions