Not class selector in jQuery
Last Updated :
31 May, 2019
Given a list of elements and the task is to not select a particular class using JQuery.
Example 1: In this example first all classes of starting
GFG- are selected then class
GFG-1 is removed from the selection using
.not() method.
html
<!DOCTYPE HTML>
<html>
<head>
<title>
Not class selector in jQuery.
</title>
<script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
</head>
<body style = "text-align:center;">
<h1 id = "h" style = "color:green;" >
GeeksForGeeks
</h1>
<p id = "GFG" style =
"font-size: 15px; font-weight: bold;">
click on button to change the text content
of all classes except GFG-1
</p>
<p class = "GFG-1" style =
"font-size: 15px; font-weight: bold;">
GFG-1
</p>
<p class = "GFG-2" style =
"font-size: 15px; font-weight: bold;">
GFG-2
</p>
<p class = "GFG-3" style =
"font-size: 15px; font-weight: bold;">
GFG-3
</p>
<button id = "button">
Click here
</button>
<p class = "GFG" style =
"color:green; font-size: 20px; font-weight: bold;">
</p>
<script>
$("button").on('click', function() {
$('p[class^="GFG-"]').not('.GFG-1').text("new Content");
$(".GFG").text("Text content changed")
});
</script>
</body>
</html>
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: In this example, first all classes of starting
GFG- are selected then class
GFG-1 is removed from the selection using
:not selector.
html
<!DOCTYPE HTML>
<html>
<head>
<title>
Not class selector in jQuery.
</title>
<script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
</head>
<body style = "text-align:center;">
<h1 id = "h" style = "color:green;" >
GeeksForGeeks
</h1>
<p id = "GFG" style =
"font-size: 15px; font-weight: bold;">
click on button to change the text content
of all classes except GFG-1
</p>
<p class = "GFG-1" style =
"font-size: 15px; font-weight: bold;">
GFG-1
</p>
<p class = "GFG-2" style =
"font-size: 15px; font-weight: bold;">
GFG-2
</p>
<p class = "GFG-3" style =
"font-size: 15px; font-weight: bold;">
GFG-3
</p>
<button id = "button">
Click here
</button>
<p class = "GFG" style =
"color:green; font-size: 20px; font-weight: bold;">
</p>
<script>
$("button").on('click', function() {
$('p[class^="GFG-"]:not(.GFG-1)').text("new Content");
$(".GFG").text("Text content changed")
});
</script>
</body>
</html>
Output:
-
Before clicking on the button:
-
After clicking on the button: