Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to use DOM Element's attributes with custom attribute names

Is it considered good practice to use DOM Element's getAttribute/setAttribute calls to associate additional information about contents of the element ?

For example I want to call setAttribute("MY_ATTRIBUTE_VALUE", "..."), where MY_ATTRIBUTE_VALUE isn't anything applicable to <div>.

Thanks !

like image 396
ikaushan Avatar asked Jan 17 '26 01:01

ikaushan


2 Answers

You should definitely go with data attributes. Here's an article on them. HTML5 Custom Data Attributes.

like image 179
Bill Criswell Avatar answered Jan 19 '26 16:01

Bill Criswell


It is very good practise as long as you're setting custom data attributes, whose purpose is holding meta data about those elements. Data attributes take the form data-name where name can be any valid descriptor.

Traditionally people would add classes, and in some cases this is still appropriate (e.g. when a class describes the state of an attribute and also denotes a style class, for which class is primarily used).

like image 32
davin Avatar answered Jan 19 '26 16:01

davin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!