Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use *= operator in jQuery attribute selector?
The *= operator is used to filter elements for attribute containing the given value. This operator selects elements where the specified attribute contains the substring anywhere within its value.
Syntax
The basic syntax for using the *= operator in jQuery attribute selector is ?
$("[attribute*='value']")
This will select all elements where the specified attribute contains the substring value.
Example
You can try to run the following code to learn how to use the *= operator to filter attributes on the basis of text ?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div[myattr*='subject']").css("background-color", "yellow");
});
</script>
</head>
<body>
<div myid="sub1" myattr="javasubject">Java</div>
<div myid="sub2" myattr="htmlsubject">HTML</div>
<div myid="sub3" myattr="ruby">Ruby</div>
</body>
</html>
In this example, the selector div[myattr*='subject'] will match the first two div elements because their myattr attribute values ("javasubject" and "htmlsubject") both contain the substring "subject". The third div with myattr="ruby" will not be selected since it doesn't contain "subject".
The output will show the Java and HTML divs with a yellow background color, while the Ruby div will remain unchanged.
Conclusion
The *= operator in jQuery attribute selectors is useful for selecting elements where an attribute contains a specific substring, providing flexible matching capabilities for dynamic content filtering.
