Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"All but not" jQuery selector

I can select (using jQuery) all the divs in a HTML markup as follows:

$('div') 

But I want to exclude a particular div (say having id="myid") from the above selection.

How can I do this using Jquery functions?

like image 657
siva636 Avatar asked Oct 29 '11 10:10

siva636


People also ask

How to use not in jQuery selector?

jQuery :not() SelectorThe :not() selector selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).

What is not () in jQuery?

The not() is an inbuilt function in jQuery which is just opposite to the filter() method. This function will return all the element which is not matched with the selected element with the particular “id” or “class”. Syntax: $(selector).not(A) The selector is the selected element which is not to be selected.


1 Answers

Simple:

$('div').not('#myid'); 

Using .not() will remove elements matched by the selector given to it from the set returned by $('div').

You can also use the :not() selector:

$('div:not(#myid)'); 

Both selectors do the same thing, however :not() is faster, presumably because jQuery's selector engine Sizzle can optimise it into a native .querySelectorAll() call.

like image 99
Bojangles Avatar answered Oct 17 '22 01:10

Bojangles