.attr() (since 1.0) gets or sets HTML attributes on matched elements — values that appear in markup like title, href, src, and alt.
Remember
// 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);
Pick the overload that matches get, set-one, set-many, or compute-per-element.
Select the element(s) with a jQuery selector.
Get: .attr("name") — returns the first match (or undefined if unset, since 1.6).
Set one: .attr("name", value) — updates every matched element.
Set many: pass a plain object of name/value pairs.
Compute: .attr("name", function (index, old) { return next; }).
Signature
Since
Role
.attr(name)
1.0
Get attribute on first element
.attr(name, value)
1.0
Set attribute; null removes it
.attr({ … })
1.0
Set several attributes at once
.attr(name, fn)
1.1
Set from function(index, oldValue)
Pseudocode
Pseudocode
if called as .attr(name):
return attribute name on first matched element
// undefined if the attribute was never set (jQuery 1.6+)
if called as .attr(name, value):
for each matched element:
if value is null: remove attribute
else: set attribute name = value
if called as .attr(object):
for each key in object:
set that attribute on every matched element
if called as .attr(name, fn):
for each matched element with index i:
next = fn(i, currentAttribute)
if next is not undefined: set attribute to next
Reference
Cheat Sheet
Keep these facts handy while reading or writing attribute code.
After toggle (typical jQuery 3.x):
attr: checked | prop: true/false | :checked: true/false
Use prop / :checked for live state.
How It Works
Listen. Update the readout whenever the checkbox changes.
attr. Shows the initial attribute string (does not track clicks).
prop. Boolean live state — prefer this for “is it checked?”.
:checked. Selector form of the same live test.
Edge Cases & Pitfalls
These trips up beginners most often.
boolean
Do not use .attr("checked") for live state
The attribute maps to the initial default, not the current click. Use .prop("checked") or .is(":checked").
syntax
Always quote "class"
class is reserved. Write .attr("class", "active"), or prefer class helper methods.
remove
null removes; ARIA false is special
.attr(name, null) removes. In jQuery 4.0+, false removes non-ARIA attrs but stringifies aria-* to "false".
getter
Getter is first-element only
Loop with .each() or .map() when you need every match’s attribute.
Compatibility
Browser Support
.attr() has been part of jQuery since 1.0. Attribute/property split clarified in 1.6 with .prop(). Works wherever your loaded jQuery build runs.
✓ Stable · jQuery 1.0+
jQuery .attr()
Universal across browsers that run your jQuery version. Prefer current jQuery 3.x+ for consistent attribute handling.
100%jQuery core
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.attr()Universal
Bottom line: Safe for everyday apps. Use .attr() for markup attributes; switch to .prop() for live boolean and form state.
Analysis
Performance Notes
Approach
Cost
Best for
One .attr() get/set
Cheap
Typical UI wiring
Object setter (few keys)
Cheap
Batching related attributes
Huge loops calling .attr()
Costly
Prefer one selection + callback setter
Attribute access is inexpensive. Prefer one selection and a callback or object setter over repeated queries inside tight loops.
Remember
Key Takeaways
Role: get/set HTML attributes on matched elements.
vs prop: attributes are markup; properties are live DOM state.
Forms: name/value, object, or callback — getters are first-only.
Remove:.attr(name, null) or .removeAttr(name).
One line:.attr() talks to HTML attributes — use .prop() when you need the live property.
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.