Understanding Relationships Between HTML Attributes & DOM Object’s Properties
Last updated
Last updated
When the web browser , it generates the corresponding DOM objects based on the DOM nodes of the document.
For example, if a page contains the following input
element:
The web browser will generate an HTMLInputElement
object.
The input
element has two attributes:
The type
attribute with the value text
.
The id
attribute with the value username
.
The generated HTMLInputElement
object will have the corresponding properties:
The input.type
with the value text
.
The input.id
with the value username
.
In other words, the web browser will automatically convert attributes of HTML elements to properties of DOM objects.
However, the web browser only converts the standard attributes to the DOM object’s properties. The standard attributes of an element are listed on the element’s specification.
Attribute-property mapping is not always one-to-one. For example:
In this example, the secured
is a non-standard attribute:
To access both standard and non-standard attributes, you use the following methods:
The element.attributes
property provides a live collection of attributes available on a specific element. For example:
Output:
When a standard attribute changes, the corresponding property is auto-updated with some exceptions and vice versa.
Suppose that you have the following input
element:
The following example illustrates the change of the tabindex
attribute is propagated to the tabIndex
property and vice versa:
The following example shows when the value
attribute changes, it reflects in the value
property, but not the other way around:
The value of an attribute is always a string. However, when the attribute is converted to the property of a DOM object, the property value can be a string, a boolean, an object, etc.
The following checkbox
element has the checked
attribute. When the checked
attribute is converted to the property, it is a boolean value:
The following shows an input
element with the style
attribute:
The style
attribute is a string while the style
property is an object:
Output:
If you want to add a custom attribute to an element, you should prefix it with the data-
e.g., data-secured
because all attributes start with data-
are reserved for the developer’s uses.
To access data-*
attributes, you can use the dataset
property. For example, we have the following div
element with custom attributes:
The following shows how to access the data-*
attributes via the dataset
property:
Output:
Attributes are specified in HTML elements.
Properties are specified DOM objects.
Attributes are converted to properties respectively.
Use the element.attributes
property to access standard and custom attributes of an element.
Use the element.dataset
property to access the data-*
attributes.
element.getAttribute(name)
–
element.setAttribute(name, value)
–
element.hasAttribute(name)
–
element.removeAttribute(name)
–
Note that element.attributes
is a NamedNodeMap
, not an , therefore, it has no Array’s methods.