The Wayback Machine - https://web.archive.org/web/20240909122852/https://www.geeksforgeeks.org/how-to-select-a-div-with-a-certain-class-using-jquery/
Open In App

How to select a div with a certain class using jQuery ?

Last Updated : 27 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 Selector and then use :not selector to ignore the elements of specific class. 

  • Example: This example implements the above approach.

html




<!DOCTYPE HTML>
<html>
 
<head>
    <title>
        Select a div with a certain class,
        that doesn't have another class.
    </title>
    <style>
        body {
            text-align: center;
        }
         
        h1 {
            color: green;
        }
         
        .div {
            background: green;
            height: 50px;
            width: 200px;
            margin: 0 auto;
            color: white;
            border: 2px solid black;
        }
    </style>
    <script src =
    </script>
</head>
 
<body>
    <h1>
        GeeksforGeeks
    </h1>
     
 
<p>
          Click on button to select a div with a certain
        class, that doesn't have another class.
    </p>
 
 
   
    <!-- div elements -->
    <div class="first div">
        This is first Div
    </div>
    <br>
    <div class="second div">
        This is second Div
    </div>
    <br>
    <div class="third div">
        This is third Div
    </div>
    <br>
    <button onClick="GFG_Fun()">
        click here
    </button>
    <br>
    <p id="geeks">
    </p>
 
 
    <script>
        
        /* main function */
        function GFG_Fun() {
           
            /* using the :not selector */
            $('.div:not(.first)')
            .css("background-color", "#173F5F");
            $('#geeks')
            .text("DIV Box of class 'first' is not selected.");
        }
    </script>
</body>
 
</html>


  • Output: 
     

Approach 2: First, select the DIV with certain class using jQuery Selector and then Use .not() method to ignore the elements of specific class.

  • Example: This example implements the above approach.

html




<!DOCTYPE HTML>
<html>
 
<head>
    <title>
        Select a div with a certain class,
        that doesn't have another class.
    </title>
    <style>
        body {
            text-align: center;
        }
         
        h1 {
            color: green;
        }
         
        .div {
            background: green;
            height: 50px;
            width: 200px;
            margin: 0 auto;
            color: white;
            border: 2px solid black;
        }
    </style>
    <script src =
    </script>
</head>
 
<body>
    <h1>
        GeeksforGeeks
    </h1>
     
 
<p>
        Click on button to select a div with a certain
        class, that doesn't have another class.
    </p>
 
 
 
    <!-- div elements -->
    <div class="first div">
        This is first Div
    </div>
    <br>
    <div class="second div">
        This is second Div
    </div>
    <br>
    <div class="third div">
        This is third Div
    </div>
    <br>
    <button onClick="GFG_Fun()">
        click here
    </button>
    <br>
    <p id="geeks">
    </p>
 
 
    <script>
         
        /* main function */
        function GFG_Fun() {
             
            /* using the .not() method */
            $('.div').not('.first')
            .css("background-color", "#173F5F");
            $('#GFG_DOWN')
            .text("DIV Box of class 'first' is not selected.");
        }
    </script>
</body>
 
</html>                   


  • Output:



Similar Reads

How React handle or restrict Props to certain types, or require certain Props to exist ?
In React, data can be transmitted from a parent component to its children using props. PropTypes, a feature in React, facilitates runtime type checks on these props, issuing warnings in the browser console if the checks fail. This ensures data consistency and helps catch potential issues during development. Syntax: import PropTypes from 'prop-types
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 copy the content of a div into another div using jQuery ?
Given an HTML document containing some div element and the task is to copy a div content into another div as its child using jQuery. There are two approaches to solve this problem that are discussed below: Approach 1: First, select the div element which needs to be copied into another div element.Select the target element where the div element is c
2 min read
How to select elements not having a certain class or attribute in CSS ?
"Is it possible to choose elements that do not have a specific class or attribute? or "Is it possible for a CSS selector selecting elements not having a certain class or attribute". It is a query that we are going to discuss, the answer to that particular problem statement is yes, and we will discuss how to achieve it. In this article, we will try
3 min read
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 Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to overlay one div over another div using CSS
Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS
2 min read
How to force child div to be 100% of parent div's height without specifying parent's height?
When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem
3 min read
How to center a div within another div?
The div tag is used to construct a division or section of an HTML document in which other elements of HTML is placed and that division/section works like a container whose CSS styling can be done as a single unit or javascript can be used to perform various tasks on that container. Syntax: &lt;div&gt; &lt;h3&gt;'This is a div of the web page.&gt;/h
4 min read
How to center a div on the edge of another div in SASS ?
In this article, we will see how to centre a div on the edge of another div using SASS. SASS is an extension of CSS that stands for Syntactically Awesome Style Sheets. It helps us to create things like variables, nested rules, etc. So that we can reuse the code. Centering a div on the edge of another div is not a difficult task. We can achieve it b
4 min read
How to make Parent Div Activate Styling of Child Div for Hover and Active ?
In this article, we will see how to make parent div activate styling of child div for hover and active. We can do this by using Pseudo-classes like hover and active. Approaches to make parent div activate styling of child div for hover and activeUsing Pure CSS: with pseudo-classesUsing CSS and JavaScript: without pseudo-classesMethod 1: Using Pure
2 min read
How to make div fixed after scroll to that div ?
In this article, we will see how to make a div fixed after you scroll to it by using the position property with a sticky value. This effect is generally used on websites when an important message needs to be shown. Set the property position to sticky to create elements that behave as fixed positions whenever the user scrolls to that position. Fixin
4 min read
How to clear all div's content inside a parent div ?
In this article, we are given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To accomplish this task, we can use the following methods: Table of Content jQuery remove() methodjQuery empty() methodjQuery html() methodjQuery remove() methodThe remove() method in jQuery is used to remove the
2 min read
How to Overlay/Hover Div in Flexbox Container Div?
In CSS, overlay/hover effects can be achieved by using Flexbox containers to center content and applying styles to reveal overlays on hover. By positioning elements absolutely within a Flexbox container, overlays can cover the entire content area. These are the following approaches: Table of Content Using ::before Pseudo-elementUsing an Overlay Div
3 min read
How to check an element has certain CSS style using jQuery ?
Given an HTML document containing some CSS property and the task is to check whether an element has a specific CSS style or not with the help of jQuery. Approach 1: Use css() method to check an element contains certain CSS styles or not. Example: This example uses css() method to check an element contains certain CSS style or not. &lt;!DOCTYPE HTML
2 min read
How to Show/Hide a Div Based on Selected Option Value of a Select Using JavaScript ?
We are going to discuss how can we show/hide a div based on a selected option value. Select is one of the input type tags. It is used in HTML to get input from one of the options given below. Not only are we able to get select input (optional input), but we can also make dynamic changes to the website according to user user-selected Options. Let's
3 min read
How to insert new row at a certain index in a table in jQuery ?
Given an HTML document containing a table. The task is to insert a new row in that table at a certain index using JQuery. Approach: Store the table column value &lt;td&gt; element into the variable. Then use eq() and after() method to insert the row in a table. Example 1: In this example, the row is inserted at index 1 (hardcoded). &lt;!DOCTYPE HTM
2 min read
How to select all elements without a given class using jQuery ?
All elements that do not match the given selector can be selected using jQuery( ":not(selector)" ). For example, the first example selects all the &lt;li&gt; elements which are not active. Example 1: This example selects &lt;li&gt; elements which does not contains the class active. &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; How t
2 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 replace innerHTML of a div using jQuery?
For replacing innerHTML of a div with jquery, html() function is used. Syntax: $(selector).html() Example: &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;title&gt; changing-innerhtml-using-jquery &lt;/title&gt; &lt;link href= &quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css
1 min read
How to fadeOut and remove a div using jQuery ?
Given a div element. The task is to remove it with a fadeOut effect using JQuery. Here are a few methods discussed. First few methods to know. jQuery text() Method: This method sets/returns the text content of the selected elements. If this method is used to return content, it provides the text content of all matched elements (HTML tags will be rem
4 min read
How to animate div width and height on mouse hover using jQuery ?
In order to animate the div width and height on mouse hover, we can use the jQuery animate() method along with the mouseenter() and mouseleave() methods. .animate() method: The animate() method changes the state of the element with CSS style. Syntax: $(selector).animate({styles}, para1, para2, para3); .mouseenter() method: The mouseenter() method w
3 min read
How to display div elements using Dropdown Menu in jQuery?
In order to display data/content of a specific element by selecting the particular dropdown menu in jQuery, we can use the with following ways: Using hide() and show() methods: hide() methods : This method is used to hiding the syntax or the element of html that you want to hide. Syntax: $(selector).hide(speed, callback); show() methods : This meth
4 min read
How to detect and change the content/style of a div using jQuery ?
The changes to the html/text of a div can be detected using jQuery on() method. The on() is used to attach one or more event handlers for the selected elements and child elements in the DOM tree. The on() method is the replacement of the bind(), live() and delegate() method from the jQuery version 1.7. Syntax: $(selector).on(event, childSelector, d
2 min read
How to iterate through child elements of a div using jQuery ?
jQuery Selector can be used to find (select) HTML elements from the DOM. Once an element is selected, the jQuery children() method is called to find all the child elements of the selected element. To loop through the child elements, the jQuery each() method is used as demonstrated in the following example. Example 1: The sample HTML page contains a
3 min read
How to show/hide div element depending multiple values using Bootstrap and jQuery ?
Bootstrap and jQuery have been used together to develop interactive web applications. Both Bootstrap and jQuery are free and open source which makes it popular among developers. This article deals with showing and hiding division depending on user input with the help of Bootstrap and jQuery. Below shown are two approaches to establish the same. Fir
5 min read
How to change the color of any div that is animated using jQuery ?
In this article, we will learn to change the color of any HTML div that is animated using jQuery.We will be using the jQuery .animate() method to change the background colors of any element. The jQuery .animate() method is used to create smooth animations by changing the CSS properties of elements over time. It allows you to specify the duration, e
2 min read
How to count all elements within a div using jQuery ?
In this article, we will count all elements inside a div element using jQuery. To count all elements inside a div elements, we use find() method and length property. The find() method is used to find all the descendant elements of the selected element. It will traverse all the way down to the last leaf of the selected element in the DOM tree. Synta
1 min read
How to call a function when content of a div changes using jQuery ?
The task is to call a function when the content of a div is changed. You can achieve this task by using the in-built method known as .change(). In simple words whenever the field is been changed we call a function that will throw a pop-up. .change(): This method will only trigger when some change occurs in the element or field. It accepts an option
2 min read
How to create a div using jQuery with style tag ?
Creating an &lt;div&gt; element with a style tag using jQuery can be done in the following steps. Steps: Create a new &lt;div&gt; element.Create an object with all the styles that we want to apply.Choose a parent element, where to put this newly created element.Put the created div element into the parent element. Example 1: This example will create
2 min read