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 jQuery selector for the elements with an ID that ends with a given string?
To get the elements with an ID that ends with a given string, use the attribute selector with the $ character. The syntax is [id$="string"] where the $ symbol indicates "ends with" and "string" is the text you want to match at the end of the ID.
Syntax
The basic syntax for selecting elements with IDs ending with a specific string is ?
$("[id$='ending_string']")
Example
You can try to run the following code to learn how to use jQuery selector for the elements with an ID that ends with a given string. Let's say we are going for elements with an ID ending with the string "new" ?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("[id$=new]").css("background-color", "yellow");
});
</script>
</head>
<body>
<div id="id1new" myattr="javasubject">Java</div>
<div id="myid1" myattr="htmlsubject">HTML</div>
<div id="id2new" myattr="rubysubject">Ruby</div>
</body>
</html>
The output of the above code is ?
The elements with IDs "id1new" and "id2new" will have a yellow background color, while the element with ID "myid1" will remain unchanged since it doesn't end with "new".
Conclusion
The jQuery attribute selector with $ symbol provides an efficient way to select elements whose IDs end with a specific string pattern. This is particularly useful when working with dynamically generated content where IDs follow naming conventions.
