jQuery .attr() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Attributes

Definition

.attr() (since 1.0) gets or sets HTML attributes on matched elements — values that appear in markup like title, href, src, and alt.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// Get (first element only)
var title = $("em").attr("title");

// Set one
$("#link").attr("title", "Market stall");

// Set many
$("#link").attr({ title: "Market stall", rel: "nofollow" });

// Remove
$("#link").attr("title", null);
CallWhat it does
.attr(name)Get attribute on first element
.attr(name, value)Set attribute; null removes it
.attr({ … })Set several attributes at once
.attr(name, fn)Set from function(index, oldValue)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
nameUsed in .attr(name). Get attribute on first element
valueUsed in .attr(name, value). Set attribute; null removes it
objectUsed in .attr({ … }). Set several attributes at once
fnUsed in .attr(name, fn). Set from function(index, oldValue)

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Get an Attribute

Read the title of the first em — official getter pattern.

jQuery
var title = $("em").attr("title");
$("#out").text("Title: " + title);
Try It Yourself

How It Works

  1. Select. $("em") finds emphasis elements.
  2. Get. .attr("title") reads the first match.
  3. Missing. Unset attributes return undefined (1.6+).

Example 2 — Set One Attribute

Pass a name and value to update every matched element.

jQuery
$("#photo").attr("title", "Beijing Brush Seller");
$("#out").text($("#photo").attr("title"));
Try It Yourself

How It Works

  1. Target. Select the element to update.
  2. Set. .attr("title", value) writes the attribute.
  3. Chain. Setters return the jQuery object for chaining.

Example 3 — Set Several Attributes

Pass a plain object to set multiple attributes in one call.

jQuery
$("#photo").attr({
  title: "jQuery Logo",
  rel: "nofollow"
});

$("#out").text(
  $("#photo").attr("title") + " | " + $("#photo").attr("rel")
);
Try It Yourself

How It Works

  1. Object. Each key is an attribute name.
  2. Apply. jQuery sets every pair on the selection.
  3. Quotes. Keys may be unquoted unless reserved (quote "class").

Frequently Asked Questions

.attr() gets or sets HTML attributes on matched elements. As a getter it returns the attribute value for the first element only (string or undefined). As a setter it updates attributes on every matched element by name/value, plain object, or callback.
.attr() reads and writes attributes — values from HTML markup (usually strings). .prop() reads and writes live DOM properties. For checked, selected, and disabled, use .prop() or .is(":checked") — not .attr("checked").
No. .attr(name) returns the value for only the first element. Loop with .each() or .map() to read each one. Setters apply to every matched element.
Pass null: .attr("title", null). That matches .removeAttr("title"). Since jQuery 4.0, non-ARIA attributes can also use false — null is the safest cross-version choice.
class is a reserved word in JavaScript. Always write .attr("class", "active"). For class toggles, prefer .addClass(), .removeClass(), or .toggleClass().
Use .attr() for standard HTML attributes (href, src, title, alt, id). Use .data() for app data in jQuery's data cache — it does not require writing data-* attributes for every value.

Next: .prop() Method

Learn how properties differ from attributes for live form and DOM state.

.prop() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful