jQuery not() method
The jquery not() method is opposite to filter() method. In filter method elements are filtered or reduced for the given matching criteria whereas not() method will filter those elements not matching.
Syntax
.not( selector )
For example
$(“li”).not(“.midli”)
Filter li in document with elements not having class name .midli.
Similarly
$(“div”).not(“.leftmenu”)
Select or filter all div elements that are not using class leftmenu.
Working Example of not() method of jquery
In example below, when button is clicked, the not() method will traverse through document and find all <p>. It will change the color to black for all <p>s which class is not midtext.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!DOCTYPE html>
<head>
<title>the title</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#filterP").click( function() {
$("p").not(".midtext").addClass("postFilter");
});
});
</script>
<style>
.toptext {color:green;}
.midtext {color:red;}
.bottomtext {color:orange;}
.postFilter {color:#000000;}
</style>
</head>
<body>
<p class="toptext">This is not example</p>
<p class="midtext">This is not example</p>
<p class="bottomtext">This is not example</p>
<input id="filterP" type="button" value="Apply Not() method on <p>" />
</body>
</html>
|
Leave A Comment?