Best practices for data association with HTMLElement objects?
custom-data-attribute, dom, javascript
Solution
Option #2 seems to me to be the most "standards-compliant", if that's what you're looking for; plus, it allows you to set those attributes from within the HTML while still maintaining valid markup. It's generally my preference, but it's really whatever works best for you in your situation: if it works, go with it.
Problem
I came across 3 ways to store any data with HTMLElement object. Can someone please suggest the best practice to associate any data with element object? I prefer number 3 because it doesn't set any HTML attribute as in the case of 1 and 2. It's just like setting and getting any property on the object. - Use setAttribute('nonStandardDataProperty') - Use dataset property of HTMLElement object for example dataset.x for data-xattribute - HTMLElement is object, so define any property and it will serve as data storage for that element