jQuery Attributes
What You’ll Learn
HTML attributes live in markup — href, src, title, alt, and id. jQuery’s .attr() gets and sets them with cross-browser consistency. For live form state like checked and selected, use .prop() instead.
.attr()
Get & set
Getter
First element
Setter
All matches
.prop()
Form state
Object
Bulk set
5 guides
Full index
Introduction
Every link, image, and form field in your page carries HTML attributes — the name-value pairs you write in markup. When JavaScript needs to read a tooltip from title, update an image src, or assign unique id values, jQuery attribute methods handle it cleanly across browsers.
What Are jQuery Attribute Methods?
.attr() is the primary API. Its getter form returns a string (or undefined) from the first matched element. Its setter forms — name/value, plain object, or callback — update every match. Since jQuery 1.6, .prop() handles live DOM properties separately: checked, selected, disabled, and value on form controls.
Use .attr(name) to get an HTML attribute from the first matched element (returns a string or undefined). Use .attr(name, value), a plain object, or a callback to set attributes on every match. For live form state — checked, selected, disabled — use .prop(), not .attr().
📝 Syntax
Minimal attribute get/set workflow:
// Get — first element only
var title = $("em").attr("title");
// Set — every matched element
$("img").attr({ src: "logo.png", alt: "Logo" });
// Form state — use .prop(), not .attr()
if ($("input").prop("checked")) { /* checked */ } 👀 .attr() vs .prop() vs .val() vs .removeAttr() vs .removeProp() vs .data()
Six related APIs — pick the right layer:
Attribute Method Tutorial Index
Search by method name or browse by category. Each tutorial includes syntax, try-it examples, and FAQs.
Get & Set
3 tutorialsRead and write HTML attributes with .attr(), live DOM properties with .prop(), and form values with .val().
| Method | Description | Tutorial |
|---|---|---|
.attr() | Get or set HTML attributes on matched elements — title, href, alt, id; use .prop() for checked, selected, and disabled. | Open |
.prop() | Get or set DOM properties on matched elements — checked, disabled, selectedIndex; use .val() for input values. | Open |
.val() | Get or set form element values — input, textarea, and select text; multi-select returns an array; use .prop() for checked and disabled. | Open |
Remove
2 tutorialsStrip HTML attributes and jQuery-added properties — .removeAttr() for markup, .removeProp() for custom .prop() cleanup.
| Method | Description | Tutorial |
|---|---|---|
.removeAttr() | Remove one or more HTML attributes from matched elements — space-separated names since 1.7; use .prop() for inline event handlers in IE. | Open |
.removeProp() | Remove jQuery-added properties set with .prop() — not for native checked, disabled, or selected; prefer .prop(name, false) for form state. | Open |
Conclusion
jQuery attribute methods let you read and write HTML markup values with cross-browser consistency. Start with .attr() for standard attributes — get from the first match, set on every match. Reach for .prop() when you need live form state, and .data() for application data that should not live in the DOM.
❓ Frequently Asked Questions
Start with .attr()
Learn getter/setter overloads, attr vs prop, and five try-it labs.
6 people found this page helpful
