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
Selected Reading
How do we use jQuery Attribute selector with a period?
jQuery Attribute selector is used to select elements with the specified attribute and value. You can use it with a period (.) in attribute values by utilizing appropriate attribute selector operators like *= (contains), ^= (starts with), or $= (ends with).
Syntax
The basic syntax for using jQuery attribute selector with a period is ?
$("[attribute*='.']") // Contains a period
$("[attribute^='.']") // Starts with a period
$("[attribute$='.']") // Ends with a period
Example
You can try to run the following code to learn how to use jQuery attribute selector with a period ?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
// Select elements with attribute containing a period
$("div[myattr*='.']").css("background-color", "yellow");
// Select elements with attribute starting with a period
$("div[myattr^='.']").css("border", "2px solid red");
});
</script>
</head>
<body>
<h3>jQuery Attribute Selector with Period Example</h3>
<div id="sub1" myattr="java.subject">Java (contains period)</div>
<div id="sub2" myattr=".htmlsubject">HTML (starts with period)</div>
<div id="sub3" myattr="rubysubject">Ruby (no period)</div>
<div id="sub4" myattr="python.programming.language">Python (multiple periods)</div>
</body>
</html>
The output of the above code is ?
Elements with periods in their myattr values will have: - Yellow background (all elements containing a period) - Red border (elements starting with a period) Java, HTML, and Python divs will be highlighted, while Ruby remains unchanged.
Conclusion
jQuery attribute selectors work seamlessly with periods in attribute values using operators like *=, ^=, and $= to match different patterns containing periods.
Advertisements
