The Wayback Machine - https://web.archive.org/web/20240910073437/https://www.geeksforgeeks.org/how-to-select-all-elements-without-a-given-class-using-jquery/
Open In App

How to select all elements without a given class using jQuery ?

Last Updated : 07 Nov, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

All elements that do not match the given selector can be selected using jQuery( “:not(selector)” ). For example, the first example selects all the <li> elements which are not active.

Example 1: This example selects <li> elements which does not contains the class active.




<!DOCTYPE html>
<html>
      
<head>
    <title>
        How to select all elements without
        a given class using jQuery ?
    </title>
      
    <meta charset="utf-8">
      
    <script src=
    </script>
</head>
  
<body>
    <h1 style="color:green">GeeksforGeeks</h1>
      
    <b>
        Select all elements without a<br>
        given class using jQuery
    </b>
      
    <ul>
        <li class="active">element 1</li>
        <li>element 2</li>
        <li>element 3</li>
        <li>element 4</li>
    </ul>
      
    <script>
        $("li:not(.active)").css(
                "background-color", "yellow" );
    </script>
</body>
  
</html>


Output:

It can also be done using .not(selector).

Example 2: This example selects <div> elements which does not contains the class green or id blue.




<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
      
    <script src=
    </script>
      
    <style>
        div {
            width: 50px;
            height: 50px;
            margin: 10px;
            float: left;
            border: 2px solid black;
        }
        .green {
            background: #8f8;
        }
        .orange {
            background: orange;
        }
        #blue {
            background: #99f;
        }
    </style>
</head>
  
<body>
    <h1 style="color:green">GeeksforGeeks</h1>
      
    <b>
        Select all elements without a
        given class using jQuery
    </b>
    <br><br>
  
    <div></div>
    <div id="blue"></div>
    <div></div>
    <div class="green"></div>
    <div class="green"></div>
    <div class="orange"></div>
    <div></div>
      
    <script>
        $("div").not(".green, #blue")
            .css("border-color", "red");
    </script>
</body>
  
</html>


Output:



Similar Reads

How to remove all the options of a select box and then add one option and select it using JQuery ?
Given a select box with a fixed number of options, the task is to remove all the options and then add one option and immediately select that particular option using jQuery. There are two approaches that can be taken to achieve this: Approach 1: Using find(), remove(), end() and append() Methods: In the following example, a select box with some opti
3 min read
How to select all elements that contains some specific CSS property using jQuery ?
Given a HTML document containing elements and some elements have specific CSS properties. The task is to select all elements having the same property value pair in them. There are two approaches that are discussed below: Approach 1: First select the elements in which we want to check for the same properties using jQuery Selector. Use filter() metho
3 min read
How to select all visible or hidden elements in a HTML page using jQuery ?
In order to select all visible or hidden elements in a page using jQuery, we can use the following jQuery selectors: :visible Selector The visible Selector is used to select all the elements that are currently visible in the document. Syntax: $(":visible") :hidden Selector The hidden selector selects hidden elements to work upon. Syntax: $(":hidden
2 min read
How to select all sibling elements of an element using jQuery ?
In this article, we will discuss how to select all sibling elements of an element using jQuery. To select all sibling elements of an element, we will use the siblings() method. The siblings() method is used to find all sibling elements of the selected element. The siblings are those having the same parent element in DOM Tree. Syntax: $(selector).si
2 min read
How to select all next sibling elements of an element using jQuery ?
In this article, we will discuss how to select all the next sibling elements of an element using jQuery. To select all the next sibling elements of an element, we will use nextAll() method. The nextAll() method is used to find the next sibling elements of the selected element. The siblings are those having the same parent element in DOM Tree. This
2 min read
How to Select all Checkboxes in all Pages in jQuery DataTable ?
Selecting all the table entries together at the same time in the jQuery data table can be achieved using the below methods. Before directly going to the implementation, first include the below CDN Links to your HTML document to implement the data table. Using the fnGetNodes() method of dataTableIn this approach, we will use the fnGetNodes() method
5 min read
How to select a div with a certain class using jQuery ?
Given a HTML document containing many div element with classes. Here the task is to select a div with a certain class, that doesn't have another class. There are two approaches to solve this problem. In one of them, we will use a method and in the other one, we will use not selector. Approach 1: First, select the DIV with certain class using jQuery
3 min read
How to Select a Range of Elements using jQuery ?
Given an HTML document with a range of similar elements and the task is to select a range of those elements with the help of JavaScript. There are two approaches that are discussed below with an example. Approach 1: First, select all elements of class = 'child' by jQuery selector then use slice() method to select a range of elements continuously. T
2 min read
How to select multiple elements using jQuery ?
In this article, we will learn how to select multiple elements using JQuery. JQuery is the fastest and most 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 simpl
2 min read
How to Select and Manipulate CSS pseudo-elements using JavaScript / jQuery ?
In this article, we will learn how to select and manipulate CSS pseudo-elements using JavaScript (or jQuery). CSS pseudo-elements allow developers to style and enhance specific parts of an HTML document with the help of selectors like::before and::after, which provide the flexibility to add decorative content, modify layouts, and create unique visu
2 min read
How to select elements with no visible children using jQuery ?
In this article we are going to learn how to select elements whose property is not visible or hidden. It simply means that the display property of that particular element is hidden, and we need to show whatever present in that element using the Jquery. We can easily do this by using the Jquery:hidden selector. First, let's see how you will know tha
2 min read
How to select all on focus in input using JQuery?
The select() event is applied to an element using jQuery when the user makes a text selection inside an element or on focus the element. This event is limited to some fields. Syntax: $("selected element").select(); //Select all Input field on focus $("input").select(); //Select Input field on focus whose type="text" $("input[type='text']").select()
1 min read
How to select all even/odd rows in table using jQuery ?
In this article, we will see how to make a table by selecting the alternate rows i.e. selecting the even or odd rows by clicking on the respective buttons. This feature can be useful at the time of selecting the specific data/elements of either of the rows or to highlight the table of data for display from the large data set. This enables us to qui
3 min read
How to select all links inside the paragraph using jQuery ?
In this article, we will see how to write code to select all links inside the paragraph using jQuery. To select all links inside paragraph element, we use parent descendant selector. This selector is used to selects every element that are descendant to a specific (parent) element. Syntax: $("parent descendant") Approach: Here, we have created links
1 min read
Select different values on two select tags in Vue.js
In this article, we will see the selection of different values on a single select and two &lt;select&gt; tags in vue.js, along with understanding their basic implementation through the examples. HTML &lt;select&gt; tag is used to create a drop-down list. Adding a list of options inside the drop-down list using the &lt;option&gt; tag. when we select
6 min read
How to select elements by attribute in jQuery ?
jQuery is a lightweight JavaScript library. In the vanilla JavaScript language, the getElementById method is used to select an element. However, jQuery provides a much lighter alternative for the same purpose. The 'jQuery Selector' allows the user to manipulate HTML elements and the data inside it(DOM manipulation). Syntax $("[attribute=value]") He
2 min read
How to select all ancestors of an element in jQuery ?
In this article, we will select all the ancestor elements of an element in jQuery. To select all ancestor elements of an element, the parents() method is used. This method is used to find all the parent elements related to the selected element. This method traverses all the levels up the selected element and returns all elements. Syntax: $(selector
1 min read
How to select all child elements recursively using CSS?
Using the child selector, you can select child elements. To select all child elements recursively, use the universal selector(*). A child selector matches when an element is the child of some element. A child selector is made up of two or more selectors separated by "&gt;". It is also known as element &gt; element selector. It selects all elements
2 min read
How to display title in Bootstrap-Select without empty element ?
In this article, we will cover how to display the title in the Bootstrap-Select without any empty element in the select dropdown menu. Bootstrap Select is a form control that displays a collapsible list of different values that can be selected. This can be used for displaying forms or menus to the user. Example 1: Using data-hidden attribute in the
2 min read
How to remove options from select element using jQuery ?
The task is to remove the option elements from the select element using jQuery. Approach: Select the option from select which needs to remove.Use JQuery remove() method to remove the option from the HTML document. Example 1: This example removes the option of which val = 'val_1' using remove() method. C/C++ Code &lt;script src= &quot;https://ajax.g
2 min read
How to select ID that starts with certain character using jQuery ?
Given an HTML document and the task is to select the elements with ID starts with certain characters using jQuery. Approach: Use jQuery [attribute^=value] Selector to select the element with ID starts with certain characters. Example 1: This example selects that element whose ID starts with 'GFG' and change their background color. &lt;!DOCTYPE HTML
2 min read
How to select first element in the drop-down list using jQuery ?
Given a drop-down list containing options elements, the task is to get the first element of the select element using JQuery. Approach 1: Select the first element of &lt;select&gt; element using the jQuery selector.Use the prop() method to get access to properties of that particular element.Change the selectedIndex property to 0 to get access to the
2 min read
How to fire an event on file select using jQuery ?
Given an HTML document and the task to fire an event when a file is selected using jQuery. The JQuery change() method is used to fire an event when file is selected. Using change() method: It is used to change the value of the input fields. This method works with "input, textarea and select" elements. Syntax: $(selector).change(function) Example 1:
2 min read
How to select text nodes using jQuery ?
Text nodes are a type of node that denotes the actual text inside an element. The textNodes of any element can be selected using jQuery by selecting all the nodes and using the filter() method to check the nodeType property. The required element is first selected using the jQuery selector. The contents() method is used on selected elements. This me
2 min read
How to select html element using ID in jQuery when ID contains a dot character ?
The task is to select an HTML element/node by ID using jQuery when the ID itself contains a DOT (' . '). Not only dots(.) there are so many other meta character such as ! " # $ % & ' ( ) * + , . / : ; ? @ [ \ ] ^ ` { | } ~ can be selected by this procedure. Syntax: $('#ID_PREFIX\\.ID_SUFFIX') Approach: In order to select an HTML element by ID we ju
2 min read
How to get N options from the &lt;select&gt; element using JQuery?
The task is to get the random N options from the select element. Below are few of the approaches: Approach 1: First get the all option element's text in the array. Generate a random index of the array and get the option of that index. Swap the last element with the current random index and subtract the length of array by 1. Repeat the process until
3 min read
How to select an element with multiple classes using jQuery ?
There are two procedures to select an element with multiple classes by using jQuery. Both the procedures are described below with the proper example. Using filter() Method: By using filter() method we can filter out all the elements that do not match the selected criteria and those matches will be returned. Syntax: $(selector).filter(criteria, func
2 min read
How to select the last row of a table using jQuery ?
Given an HTML table and the task is to select the last row of the table with the help of jQuery. There are two approaches that are discussed below with the proper example. Approach 1: First, select the table by its ID. Use find() method to find the all table rows of the table. Use last() method to get the last row of table. The background color of
2 min read
How to detect if a dropdown list is a multi-select using jQuery?
In this article, we will learn how to detect if a dropdown list or an HTML select element is a multi-select one or not using jQuery. There is one main approach with which this can be implemented. Approach: Using the prop() method in the jQuery library. There is one select element with an id of dropdown defined in the following example which also ha
3 min read
How to create a Disabled Option Select using jQuery Mobile ?
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be creating a Disabled Option Select using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. &lt;link rel="stylesheet" href="http://code.jquery.com/mobile/1
1 min read