"ALL but NOT" jQuery selector
Requirement:
We can select (using jQuery) all the divs in a HTML markup as follows:
$('div')
But If we want to exclude a particular div (say having id="myid"
) from the above selection. Then too it's simple, here is the solution.
Solution:
Use .not
selector from jQuery as following.
$('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 task; however, :not()
is quicker, likely because jQuery's Sizzle selector engine can optimise it to perform as a native. calling .querySelectorAll()
.
Thanks!