Javascript DOM: Setting custom DOM element properties

As a general rule, don’t use custom properties. You shouldn’t modify DOM objects in ways they don’t expect because they may not behave the way you think they will.

The mechanism for custom attributes in HTML5 is to use the data- prefix.

However, HTML5 is not a stanard and is not that widely implemented yet. But if you use set/getAttribute for data- attributes it should work on all reasonably modern browsers and no standard attribute should be introduced in the future with a data- prefix.

But having said that, I would still recommend using a custom object to store the values and referencing them by element id or class or some other standard (as in HTML 4.01) attribute value. It avoids the issue of custom properties and attributes and is known to work everywhere.

Leave a Comment