.attr()
Get the value of an attribute for the first element in the set of matched elements also, Set one or more attributes for the set of matched elements.
Method Details
Method Call | Description |
---|---|
.attr( attributeName ) | attributeName Type: String The name of the attribute to get. |
.attr( attributeName, value ) | attributeName Type: String The name of the attribute to set. value Type: String or Number or Null A value to set for the attribute. If null, the specified attribute will be removed. |
.attr( attributes ) | attributes Type: PlainObject An object of attribute-value pairs to set. |
.attr( attributeName, function ) | attributeName Type: String The name of the attribute to set function Type: Function( Integer index, String attr ) => String or Number A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old attribute value as arguments. |
The .attr() method gets the attribute value for only the first element in the matched set.
Find the title attribute of the first <em>
in the page.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>attr demo</title>
<style>
em {
color: blue;
font-weight: bold;
}
div {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.3.js"></script>
</head>
<body>
<p>Once there was a <em title="huge, gigantic">large</em> dinosaur...</p>
The title of the emphasis is:<div></div>
<script>
var title = $( "em" ).attr( "title" );
$( "div" ).text( title );
</script>
</body>
</html>
The .attr()
method is a convenient way to set the value of attributes—especially when setting multiple attributes or using values returned by a function. Consider the following image:
<img id="greatphoto" src="brush-seller.jpg" alt="brush seller">
Setting a simple attribute
To change the alt attribute, simply pass the name of the attribute and its new value to the .attr()
method:
$( "#greatphoto" ).attr( "alt", "Beijing Brush Seller" );
Add an attribute the same way:
$( "#greatphoto" ).attr( "title", "Photo by Kelly Clark" );
Setting several attributes at once
To change the alt attribute and add the title attribute at the same time, pass both sets of names and values into the method at once using a plain JavaScript object. Each key-value pair in the object adds or modifies an attribute:
$( "#greatphoto" ).attr({
alt: "Beijing Brush Seller",
title: "photo by Kelly Clark"
});
When setting multiple attributes, the quotes around attribute names are optional.
Computed attribute values
By using a function to set attributes, you can compute the value based on other properties of the element. For example, to concatenate a new value with an existing value:
$( "#greatphoto" ).attr( "title", function( i, val ) {
return val + " - photo by Kelly Clark";
});
This use of a function to compute attribute values can be particularly useful when modifying the attributes of multiple elements at once.
Examples:
Set some attributes for all <img>s in the page.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>attr demo</title>
<style>
img {
padding: 10px;
}
div {
color: red;
font-size: 24px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.3.js"></script>
</head>
<body>
<img>
<img>
<img>
<div><b>Attribute of Ajax</b></div>
<script>
$( "img" ).attr({
src: "/resources/hat.gif",
title: "jQuery",
alt: "jQuery Logo"
});
$( "div" ).text( $( "img" ).attr( "alt" ) );
</script>
</body>
</html>
Set the src attribute from title attribute on the image.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>attr demo</title>
<script src="https://code.jquery.com/jquery-3.6.3.js"></script>
</head>
<body>
<img title="hat.gif">
<script>
$( "img" ).attr( "src", function() {
return "/resources/" + this.title;
});
</script>
</body>
</html>