Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 56 ยท Manipulating the DOM

Attributes and Properties

Learn the difference between HTML attributes and DOM properties, when to use jQuery .attr() and .prop(), and how to handle checked, disabled and href correctly.

Every HTML element has two layers of information. The attributes are the text you write in the markup (<input type="checkbox" checked>), and the properties are the live values on the JavaScript object the browser builds from that markup. Most of the time the two layers agree, which is why beginners mix them up. But they are not the same thing, and the moments when they disagree are exactly where bugs hide.

jQuery gives you one method for each layer: .attr() reads and writes attributes, and .prop() reads and writes properties. Choosing the right one is the whole lesson. If you have not met the basic getters and setters yet, start with Getting and Setting Content; the same "one argument reads, two arguments write" pattern applies here.

Two layers of one element

Attribute What the HTML says. Starts as the initial value.
Property What the element is right now. Changes as users interact.

A user ticking a checkbox changes the property, not the attribute.

1 / 2

Reading and writing attributes with .attr()

Pass one argument to read the attribute of the first element in the collection. Pass a second argument to set it on every element.

JavaScript
$("img").attr("alt");               // read: "A sunset" (or undefined if missing)
$("img").attr("alt", "A sunset");   // write on all images

If the attribute does not exist, .attr() returns undefined, not an empty string. That makes it easy to test whether an attribute is present at all.

You can set several attributes at once by passing an object, and you can compute a value for each element with a function. The function receives the element's index and its old value:

JavaScript
$("a.external").attr({
  target: "_blank",
  rel: "noopener noreferrer"
});

$("li").attr("id", function (index) {
  return "item-" + (index + 1);
});

Use .removeAttr("name") to delete an attribute completely. You can remove several at once by separating names with spaces: .removeAttr("title alt").

Reading, setting and removing attributes

Reading and writing properties with .prop()

Properties are the values the browser keeps on the element object: checked, disabled, selected, value, tagName, scrollTop and many more. Use .prop() for them. It has the same shapes as .attr(): one argument reads, two arguments write, an object writes several, and a function computes values.

JavaScript
$("#agree").prop("checked");          // true or false
$("#agree").prop("checked", true);    // tick the box
$("button").prop("disabled", true);   // disable every button

The key difference is the type you get back. .prop("checked") is a real boolean. The matching attribute is only text that says whether checked was written in the markup. Compare what .attr() and .prop() return after a user interacts with the page.

The checkbox trap

Imagine <input type="checkbox" id="news" checked> and a user who unticks it. The attribute is still in the HTML, because the user did not edit the markup. Only the property changed to false. If your code asks .attr("checked") to find out whether the box is ticked, it will report the original markup, not what the user sees. In jQuery 3, .attr("checked") returns the string "checked" while the attribute is present, and undefined when it is not.

The rule is simple: for state that changes, use .prop(). Use .is(":checked") as a readable alternative for reading, and .prop("checked", false) to clear a box. The same applies to disabled, selected and readOnly.

Attribute versus property on a checkbox

Notice the function form in the last handler: .prop("disabled", function (i, old) { return !old; }) flips each element's own state, which is handy for a toggle.

Values that differ between attribute and property

Checkboxes are not the only place where the layers disagree.

  • href and src: .attr("href") returns exactly what you wrote (/docs/start), while .prop("href") returns the fully resolved URL (https://example.com/docs/start). Use .attr() when you need the original text and .prop() when you need a usable absolute URL.
  • value: the value attribute is the starting value of a field; the property is what the user has typed. That is why jQuery has .val(), which reads the live value.
  • class and className: the attribute is class, the property is className. You rarely touch either directly; the class methods are better.
  • style: the attribute is a string, the property is an object. Use .css() instead.

What about boolean attributes with .attr()?

jQuery treats checked, disabled, selected, readonly and similar names specially when you use .attr(). Setting them to false removes the attribute, and true sets it to its own name. That keeps older code working, but it is a compatibility feature. For new code, reach for .prop() whenever you mean state.

Removing properties

.removeProp() exists, but it is only meant for custom properties you added yourself. Removing a built-in property such as checked or disabled can break the element, so do not do it. To turn off a boolean state, set it to false with .prop(). To take a custom value away, you can also keep it in jQuery's own storage, which is the topic of Storing Data with .data().

Putting it together: a quick guide

You want to... Use
Read or change alt, title, aria-label, role .attr()
Read the link text exactly as written in HTML .attr("href")
Know whether a box is ticked now .prop("checked")
Enable or disable a control .prop("disabled", bool)
Delete an attribute from the markup .removeAttr()
Read the current typed text .val()

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A user unticks a checkbox that has the checked attribute in its HTML. Which call correctly reports the current state?

    The user changes the checked property, not the attribute in the markup. Only .prop("checked") (or .is(":checked")) reflects the live state.
  2. 2. What does .attr("alt") return when the first element has no alt attribute?

    A missing attribute reads as undefined, so you can tell it apart from an attribute that is present but empty.
  3. 3. Which line disables every button on the page the modern way?

    disabled is state, so you set the property to a real boolean. The string "false" would still leave the attribute present, and removeProp is not meant for built-in properties.
  4. 4. For <a href="/docs">, how do .attr("href") and .prop("href") differ?

    The attribute keeps your original text. The property is computed by the browser and includes the origin.

Summary

  • Attributes are the text in the markup; properties are the live values on the element object.
  • .attr() reads the first element and writes to all; a missing attribute reads as undefined.
  • Pass an object to set several attributes at once, or a function to compute one value per element.
  • Use .prop() for changing state: checked, disabled, selected. It returns real booleans.
  • .removeAttr() deletes attributes; .removeProp() is only for custom properties you added.
  • href and src differ: .attr() gives the written text, .prop() gives the resolved URL.

Next, learn how to attach your own values to elements without touching the HTML in Storing Data with .data().