jQuery [attribute] Selector
Last Updated :
27 Jun, 2023
The [attribute] Selector is an inbuilt selector in jQuery, used to select all the elements with the specified attribute.
Syntax:
$("[attribute_name]")
Parameter:
- attribute_name: It is the required parameter which specify the attribute to be select.
Example-1:
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
< script >
$(document).ready(function() {
$("[class]").css("color",
"green");
});
</ script >
</ head >
< body >
< center >
< h3 class>GeeksforGeeks</ h3 >
< p >A computer science portal for geeks</ p >
< p id>Geek1</ p >
< p class>Geek2</ p >
< p >Geek3</ p >
< div class> Thank You! </ div >
</ center >
</ body >
</ html >
|
Output:
In the above example, all the elements with the specified attribute (class) are formatted into green color i.e, “GeeksforGeeks”, “Geek2” and “Thank You!”.
Example-2:
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
< script >
$(document).ready(function() {
$("[class]").css("color",
"green");
});
</ script >
</ head >
< body >
< center >
< h3 class = "demo" >GeeksforGeeks</ h3 >
< p >A computer science portal for geeks</ p >
< p id>Geek1</ p >
< p >Geek2</ p >
< p class = "test" >Geek3</ p >
< div class = "sample" > Thank You! </ div >
</ center >
</ body >
</ html >
|
Output:
In the above example, all the elements with the specified attribute (class) are formatted into green color rather considering the value of the attribute. “GeeksforGeeks”, “Geek3” and “Thank You!” are formatted.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...