After the changes made in jQuery 1.6.1, I have been trying to define the difference between properties and attributes in HTML.
Looking at the list on the jQuery 1.6.1 release notes (near the bottom), it seems one can classify HTML properties and attributes as follows:
Properties: All which either has a boolean value or that is UA calculated such as selectedIndex.
Attributes: 'Attributes' that can be added to a HTML element which is neither boolean nor containing a UA generated value.
Thoughts?
After reading Sime Vidas's answer, I searched more and found a very straight-forward and easy-to-understand explanation in the angular docs.
When writing HTML source code, you can define attributes on your HTML elements. Then, once the browser parses your code, a corresponding DOM node will be created. This node is an object, and therefore it has properties.
For instance, this HTML element:
has 2 attributes (
type
andvalue
).Once the browser parses this code, a HTMLInputElement object will be created, and this object will contain dozens of properties like: accept, accessKey, align, alt, attributes, autofocus, baseURI, checked, childElementCount, childNodes, children, classList, className, clientHeight, etc.
For a given DOM node object, properties are the properties of that object, and attributes are the elements of the
attributes
property of that object.When a DOM node is created for a given HTML element, many of its properties relate to attributes with the same or similar names, but it's not a one-to-one relationship. For instance, for this HTML element:
the corresponding DOM node will have
id
,type
, andvalue
properties (among others):The
id
property is a reflected property for theid
attribute: Getting the property reads the attribute value, and setting the property writes the attribute value.id
is a pure reflected property, it doesn't modify or limit the value.The
type
property is a reflected property for thetype
attribute: Getting the property reads the attribute value, and setting the property writes the attribute value.type
isn't a pure reflected property because it's limited to known values (e.g., the valid types of an input). If you had<input type="foo">
, thentheInput.getAttribute("type")
gives you"foo"
buttheInput.type
gives you"text"
.In contrast, the
value
property doesn't reflect thevalue
attribute. Instead, it's the current value of the input. When the user manually changes the value of the input box, thevalue
property will reflect this change. So if the user inputs"John"
into the input box, then:whereas:
The
value
property reflects the current text-content inside the input box, whereas thevalue
attribute contains the initial text-content of thevalue
attribute from the HTML source code.So if you want to know what's currently inside the text-box, read the property. If you, however, want to know what the initial value of the text-box was, read the attribute. Or you can use the
defaultValue
property, which is a pure reflection of thevalue
attribute:There are several properties that directly reflect their attribute (
rel
,id
), some are direct reflections with slightly-different names (htmlFor
reflects thefor
attribute,className
reflects theclass
attribute), many that reflect their attribute but with restrictions/modifications (src
,href
,disabled
,multiple
), and so on. The spec covers the various kinds of reflection.The answers already explain how attributes and properties are handled differently, but I really would like to point out how totally insane this is. Even if it is to some extent the spec.
It is crazy, to have some of the attributes (e.g. id, class, foo, bar) to retain only one kind of value in the DOM, while some attributes (e.g. checked, selected) to retain two values; that is, the value "when it was loaded" and the value of the "dynamic state". (Isn't the DOM supposed to be to represent the state of the document to its full extent?)
It is absolutely essential, that two input fields, e.g. a text and a checkbox behave the very same way. If the text input field does not retain a separate "when it was loaded" value and the "current, dynamic" value, why does the checkbox? If the checkbox does have two values for the checked attribute, why does it not have two for its class and id attributes? If you expect to change the value of a text *input* field, and you expect the DOM (i.e. the "serialized representation") to change, and reflect this change, why on earth would you not expect the same from an input field of type checkbox on the checked attribute?
The differentiation, of "it is a boolean attribute" just does not make any sense to me, or is, at least not a sufficient reason for this.
well these are specified by the w3c what is an attribute and what is a property http://www.w3.org/TR/SVGTiny12/attributeTable.html
but currently attr and prop are not so different and there are almost the same
but they prefer prop for some things
well actually you dont have to change something if you use attr or prop or both, both work but i saw in my own application that prop worked where atrr didnt so i took in my 1.6 app prop =)
Difference HTML properties and attributes:
Let's first look at the definitions of these words before evaluating what the difference is in HTML:
English definition:
In HTML context:
When the browser parses the HTML, it creates a tree data structure wich basically is an in memory representation of the HTML. It the tree data structure contains nodes which are HTML elements and text. Attributes and properties relate to this is the following manner:
It is also important to realize that the mapping of these properties is not 1 to 1. In other words, not every attribute which we give on an HTML element will have a similar named DOM property.
Furthermore have different DOM elements different properties. For example, an
<input>
element has a value property which is not present on a<div>
property.Example:
Let's take the following HTML document:
Then we inspect the
<div>
, in the JS console:We see the following DOM properties (chrome devtools, not all properties shown):
class
is reserved keyword in JS). But actually 2 properties,classList
andclassName
.