Posted in: Using jQuery Core

Attributes

An element's attributes can contain useful information for your application, so it's important to be able to get and set them.

link The .attr() method

The .attr() method acts as both a getter and a setter. As a setter, .attr() can accept either a key and a value, or an object containing one or more key/value pairs.

.attr() as a setter:

1
2
3
4
5
6
$( "a" ).attr( "href", "allMyHrefsAreTheSameNow.html" );
$( "a" ).attr({
title: "all titles are the same too!",
href: "somethingNew.html"
});

.attr() as a getter:

1
$( "a" ).attr( "href" ); // Returns the href for the first a element in the document