The .attr() method returns the value of an attribute for the first element in the set of matched elements. You can also populate attributes of the specific element with .attr(). Attributes are things like alt="..." or title="..." or src="...".

Three dot cartouche

Use .attr() to fetch information

Use the .attr() method to go and fetch information about an element. This could be used to fetch the title for an image to use someplace else, as in the example below.

<img id="topimage" src="/images/bigredball.jpg" alt="Big Red Ball" />
<p id="caption"></p>

    var caption = $("#topimage").attr("alt");

Three dot cartouche

Use attr() to populate attributes for elements

The .attr() method can also be used to populate the attribute by passing a value to the specific element.

$("#caption1").attr("title", "Photo of a sea lion");  // this will add title="Photo of a sea lion" to the element with id="caption"