jQuery: select all elements of a given class, except for a particular Id

Ankur picture Ankur · Mar 31, 2010 · Viewed 123.7k times · Source

This is probably pretty simple.

I want to select all elements of a given class thisClass, except where the id is thisId.

i.e. something equivalent to (where -/minus implies remove):

$(".thisClass"-"#thisId").doAction();

Answer

rahul picture rahul · Mar 31, 2010

Use the :not selector.

$(".thisclass:not(#thisid)").doAction();

If you have multiple ids or selectors just use the comma delimiter, in addition:

(".thisclass:not(#thisid,#thatid)").doAction();