These methods get and set DOM attributes of elements.
Get the value of an attribute for the first element in the set of matched elements.
.attr(attributeName)
A string containing the attribute value.
It’s important to note that the .attr() method gets the attribute value for only the first element in the matched set. To get the value for each element individually, we need to rely on a looping construct such as jQuery’s .each() method.
Using jQuery’s .attr() method to get the value of an element’s attribute has two main benefits:
Set one or more attributes for the set of matched elements.
.attr(attributeName, value)
.attr(map)
.attr(attributeName, function)
The jQuery object, for chaining purposes.
The .attr() method is a convenient and powerful way to set the value of attributes, especially when setting multiple attributes or using values returned by a function. Let’s consider the following image:
<img id="greatphoto" src="brush-seller.jpg"
alt="brush seller" />
We can change the alt attribute by simply passing the name of the attribute and its new value to the .attr() method.
$('#greatphoto').attr('alt', 'Beijing Brush Seller');
We can add an attribute the same way.
$('#greatphoto')
.attr('title', 'Photo by Kelly Clark');
To change the alt attribute and add the title attribute at the same time, we can pass both sets of names and values into the method at once using a map (JavaScript object literal). Each key-value pair in the map adds or modifies an attribute:
$('#greatphoto').attr({
alt: 'Beijing Brush Seller',
title: 'photo by Kelly Clark'
});
When setting multiple attributes, the quotation marks around attribute names are optional.
By using a function to set attributes, we can compute the value based on other properties of the element. For example, we could concatenate a new value with an existing value as follows:
$('#greatphoto').attr('title', function() {
return this.alt + ' – photo by Kelly Clark'
});
This use of a function to compute attribute values can be particularly useful when we modify the attributes of multiple elements at once.
Remove an attribute from each element in the set of matched elements.
.removeAttr(attributeName)
.removeAttr(function)
The jQuery object, for chaining purposes.
The .removeAttr() method uses the JavaScript removeAttribute() function, but it has the advantage of being able to be called directly on a jQuery object and it accounts for different attribute naming across browsers.
As of jQuery 1.4, the .removeAttr() function allows us to indicate the attribute to be removed by passing in a function.
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…