JavaScript Attributes and Properties

When the browser loads the page, it “reads” (another word: “parses”) HTML text and generates DOM objects from it. For element nodes most standard HTML attributes automatically become properties of DOM objects.

For instance, if the tag is <body id="page">, then the DOM object has body.id="page".

But the attribute-property mapping is not one-to-one! In this chapter we’ll pay attention to separate these two notions, to see how to work with them, when they are the same, and when they are different.

DOM properties

We’ve already seen built-in DOM properties. There’s a lot. But technically no one limits us, and if it’s not enough – we can add our own.

DOM nodes are regular JavaScript objects. We can alter them.

For instance, let’s create a new property in document.body:

We can add a method as well:

We can also modify built-in prototypes like Element.prototype and add new methods to all elements:

So, DOM properties and methods behave just like those of regular JavaScript objects:

  • They can have any value.
  • They are case-sensitive (write elem.nodeType, not elem.NoDeTyPe).

HTML attributes

In HTML language, tags may have attributes. When the browser reads HTML text and creates DOM objects for tags, it recognizes standard attributes and creates DOM properties from them.

So when an element has id or another standard attribute, the corresponding property gets created. But that doesn’t happen if the attribute is non-standard.

For instance:

Please note that a standard attribute for one element can be unknown for another one. For instance, "type" is standard for <input> (HTMLInputElement), but not for <body> (HTMLBodyElement). Standard attributes are described in the specification for the corresponding element class.

Here we can see it:

So, if an attribute is non-standard, there won’t be DOM-property for it. Is there a way to access such attributes?

Sure. All attributes are accessible using following methods:

  • elem.hasAttribute(name) – checks for existence.
  • elem.getAttribute(name) – gets the value.
  • elem.setAttribute(name, value) – sets the value.
  • elem.removeAttribute(name) – removes the attribute.

These methods operate exactly with what’s written in HTML.

Also one can read all attributes using elem.attributes: a collection of objects that belong to a built-in Attr class, with name and value properties.

Here’s a demo of reading a non-standard property:

HTML attributes have the following features:

  • Their name is case-insensitive (id is same as ID).
  • Their values are always strings.

Here’s an extended demo of working with attributes:

Please note:

  1. getAttribute('About') – the first letter is uppercase here, and in HTML it’s all lowercase. But that doesn’t matter: attribute names are case-insensitive.
  2. We can assign anything to an attribute, but it becomes a string. So here we have "123" as the value.
  3. All attributes including ones that we set are visible in outerHTML.
  4. The attributes collection is iterable and has all attributes with name and value.

Property-attribute synchronization

When a standard attribute changes, the corresponding property is auto-updated, and (with some exceptions) vice versa.

In the example below id is modified as an attribute, and we can see the property changed too. And then the same backwards:

But there are exclusions, for instance input.value synchronizes only from attribute → to property, but not back:

In the example above:

  • Changing the attribute value updates the property.
  • But the property change does not affect the attribute.

That “feature” may actually come in handy, because the user may modify value, and then after it, if we want to recover the “original” value from HTML, it’s in the attribute.

DOM properties are typed

DOM properties are not always strings. For instance, the input.checked property (for checkboxes) is a boolean:

There are other examples. The style attribute is a string, but the style property is an object:

That’s an important difference. But even if a DOM property type is a string, it may differ from the attribute!

For instance, the href DOM property is always a full URL, even if the attribute contains a relative URL or just a #hash.

Here’s an example:

If we need the value of href or any other attribute exactly as written in the HTML, we can use getAttribute.

Non-standard attributes, dataset

When writing HTML, we use a lot of standard attributes. But what about non-standard, custom ones? First, let’s see whether they are useful or not? What for?

Sometimes non-standard attributes are used to pass custom data from HTML to JavaScript, or to “mark” HTML-elements for JavaScript.

Like this:

Also they can be used to style an element.

For instance, here for the order state the attribute order-state is used:

Why the attribute may be preferable to classes like .order-state-new.order-state-pendingorder-state-canceled?

That’s because an attribute is more convenient to manage. The state can be changed as easy as:

But there may be a possible problem with custom attributes. What if we use a non-standard attribute for our purposes and later the standard introduces it and makes it do something? The HTML language is alive, it grows, more attributes appear to suit the needs of developers. There may be unexpected effects in such case.

To avoid conflicts, there exist data-* attributes.

All attributes starting with “data-” are reserved for programmers’ use. They are available in the dataset property.

For instance, if an elem has an attribute named "data-about", it’s available as elem.dataset.about.

Like this:

Multiword attributes like data-order-state become camel-cased: dataset.orderState.

Here’s a rewritten “order state” example:

Using data-* attributes is a valid, safe way to pass custom data.

Please note that we can not only read, but also modify data-attributes. Then CSS updates the view accordingly: in the example above the last line (*) changes the color to blue.

Summary

  • Attributes – is what’s written in HTML.
  • Properties – is what’s in DOM objects.

A small comparison:

Properties Attributes
Type Any value, standard properties have types described in the spec A string
Name Name is case-sensitive Name is not case-sensitive

Methods to work with attributes are:

  • elem.hasAttribute(name) – to check for existence.
  • elem.getAttribute(name) – to get the value.
  • elem.setAttribute(name, value) – to set the value.
  • elem.removeAttribute(name) – to remove the attribute.
  • elem.attributes is a collection of all attributes.

For most needs, DOM properties can serve us well. We should refer to attributes only when DOM properties do not suit us, when we need exactly attributes, for instance:

  • We need a non-standard attribute. But if it starts with data-, then we should use dataset.
  • We want to read the value “as written” in HTML. The value of the DOM property may be different, for instance the hrefproperty is always a full URL, and we may want to get the “original” value.
Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *