Applicable before jQuery 1.9
Below is a list of some attributes and properties and which method should normally be used when getting or setting them. This is the preferred usage, but the .attr()
method will work in all cases.
+------------------------------------+------------+-----------+
| Attribute/Property | .attr() | .prop() |
+------------------------------------+------------+-----------+
| accesskey | ✓ | |
| align | ✓ | |
| async | | ✓ |
| autofocus | | ✓ |
| checked | | ✓ |
| class | ✓ | |
| contenteditable | ✓ | |
| disabled | | ✓ |
| draggable | ✓ | |
| href | ✓ | |
| id | ✓ | |
| label | ✓ | |
| location (i.e., window.location) | | ✓ |
| multiple | | ✓ |
| readOnly | | ✓ |
| rel | ✓ | |
| selected | | ✓ |
| src | ✓ | |
| tabindex | ✓ | |
| title | ✓ | |
| type | ✓ | |
| width (if needed over .width()) | ✓ | |
+------------------------------------+------------+-----------+
Neither .attr()
nor .prop()
should be used for getting/setting value. Use the .val()
method instead (although using .attr(“value”, “somevalue”)
will work.
Summary: The .prop()
method should be used for boolean attributes/properties and for properties which do not exist in html (such as window.location
). All other attributes (ones you can see in the html) can and should continue to be manipulated with the .attr()
method.