A co-worker suggested to change our full JavaScript code to select DOM elements only via data attribute instead of id/class names.
He says, this is bad:
$('#foo')
$('.bar')
And this is good:
$('[data-foo]')
I didn't know that there is a benefit to this so I googled up a bit and found these two blog posts:
- Contra data attributes: http://intuio.at/en/blog/dont-use-data-attributes-to-find-html-elements-with-js/
- Pro: http://roytomeij.com/blog/2012/dont-use-class-names-to-find-HTML-elements-with-JS.html
Since these blog articles are just opinions of two developers I'd like to know what's the actual practical experience with this? Is there a real benefit to using data-attributes for DOM selection or is it a stupid idea?