.attr()
method with exampleThe .attr()
method in jQuery is used to get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.
.attr()
method Syntax.attr([attributeName[,value|function]|[attributes]) // That is .attr(attributeName) .attr(attributeName,value) .attr(attributeName,function) .attr(attributes)
Parameter | Type | Description |
---|---|---|
attributeName |
String | the attribute name |
value |
String,Number,Null | a value to set for the attribute. |
function(index, attr) |
Function | a function returning attribute value. index is the position of the element in the set. attr is old attribute value. |
attributes |
object | an object of attribute-value pairs to set |
.attr(attributeName)
Get the value of an attribute for the first element
in the set of matched elements.
return value: It will return the value of src
attribute for the first img
element.
$('img').attr('src');
<img src="lautturi.com-test-1.jpg" alt="image 1"> <img src="lautturi.com-test-2.jpg" alt="image 2"> <script> console.log($('img').attr('src')); // lautturi.com-test-1.jpg </script>
.attr(attributeName,value)
Add/Set a simple attribute for the set of matched elements.
Add a title attribute for all images.
$("img").attr("title", "Test Image");
<img src="lautturi.com-test-1.jpg" alt="image 1"> <img src="lautturi.com-test-2.jpg" alt="image 2"> <script> $("img").attr("title", "Test Image"); // <img src="lautturi.com-test-1.jpg" alt="image 1" title="Test Image"> // <img src="lautturi.com-test-2.jpg" alt="image 2" title="Test Image"> </script>
.attr(attributeName,function)
Add/Set a simple attribute for the set of matched elements.
function
will return a value to set for the attribute.
Set the value of the alt
attribute to the value of the title
attribute for all images.
$("img").attr("title",function() { return this.alt });
<img src="lautturi.com-test-1.jpg" alt="image 1"> <img src="lautturi.com-test-2.jpg" alt="image 2"> <script> $("img").attr("title",function() { return this.alt }); // <img src="lautturi.com-test-1.jpg" alt="image 1" title="image 1"> // <img src="lautturi.com-test-2.jpg" alt="image 2" title="image 2"> </script>
.attr(attributeName,null)
Remove an attribute.
Remove the title attribute for all images.
// remove an attribute using jQuery $("img").attr("title",null); // or $("img").attr("title",function() { return null});
<img src="lautturi.com-test-1.jpg" alt="image 1" title="image 1"> <img src="lautturi.com-test-2.jpg" alt="image 2" title="image 2"> <script> $("img").attr("title",function() { return null }); // <img src="lautturi.com-test-1.jpg" alt="image 1"> // <img src="lautturi.com-test-2.jpg" alt="image 2"> </script>
.attr(attributes)
Add/Set one or more attributes for the set of matched elements.
attributes
is an object of attr-value pairs,for example:
{'src':'test.jpg'}
,
{ title: "test.jpg", alt: "Test Image" }
.
Set the src and alt attributes for all images.
$("img").attr({ src: "test.jpg", alt: "Test Image" });
<img src="lautturi.com-test-1.jpg" alt="image 1"> <img src="lautturi.com-test-2.jpg" alt="image 2"> <script> $("img").attr({ src: "lautturi.jpg", alt: "Test Image" }); // <img src="lautturi.jpg" alt="Test Image"> // <img src="lautturi.jpg" alt="Test Image"> </script>
For <input type="checkbox" checked="checked" />
method | result | why |
---|---|---|
elem.checked | true (Boolean) | Will change with checkbox state |
$( elem ).prop( "checked" ) | true (Boolean) | Will change with checkbox state |
elem.getAttribute( "checked" ) | "checked" (String) | Initial state of the checkbox; does not change |
$( elem ).attr( "checked" ) (1.6) | "checked" (String) | Initial state of the checkbox; does not change |
$( elem ).attr( "checked" ) (1.6.1+) | "checked" (String) | Will change with checkbox state |
$( elem ).attr( "checked" ) (pre-1.6) | true (Boolean) | Changed with checkbox state |