Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to remove elements that matches the specified selector from the set of matched elements using jQuery ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The task is to remove elements matching the specified selector from the set of matched elements using jQuery. jQuery is the fastest and lightweight JavaScript library that is used to simplify the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript.

jQuery is widely famous for its motto of Write less, do more. It simply means that you can achieve your goal by just writing a few lines of code.

Approach: We can accomplish this task by using the .not() method in jQuery. This method is used to remove elements from the set of matched elements.

Example: The following code demonstrates showing bold green border for the <div> element which are not having class as “green” neither have id as “blueone”. This code also uses the jQuery css() method.  The css() method in jQuery is used to change the style property of the selected element which is used to provide a green border to the selected elements.



<!doctype html>
<html lang="en">
    <meta charset="utf-8">
    <script src=
        div {
            width: 50px;
            height: 50px;
            margin: 10px;
            float: left;
            background: pink;
            border: 5px solid white;
        .green {
            background: #8f8;
        .gray {
            background: #ccc;
        #blueone {
            background: #99f;
        body {
            text-align: center;
    <h1 style="color:green">
    <div id="blueone">GFG</div>
    <div class="green">GFG</div>
    <div class="green">GFG</div>
    <div class="gray">GFG</div>
        $("div").not(".green, #blueone")
            .css("border-color", "Green");


My Personal Notes arrow_drop_up
Last Updated : 28 Nov, 2021
Like Article
Save Article
Similar Reads
Related Tutorials