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?
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?
You use the
.not
property of the jQuery library:See it in action here. The div #myDiv will be white.
This will include all the divs except the one with id 'myid'
this is what you need i think.
[doc]
or
[doc]
are main ways to select all but one id
You can see demo here
Simple:
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: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.You could just do it the old fashioned way. No need for jQuery with something so simple.
Pro tips:
A set of dom elements is just an array, so use your favourite
toArray
method on aNodeList
.Adding elements to a set is just
set.push.apply(set, arrOfElements);
Removing an element from a set is
set.splice(set.indexOf(el), 1)
You can't easily remove multiple elements at once :(