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?
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With