Hacker News new | ask | show | jobs
Don't use class names to find HTML elements with JS (roytomeij.com)
14 points by roytomeij 5259 days ago
4 comments

The title of this article should have been "Don't use class names to store data." Finding elements by class name is fine. This is about misusing the class attribute.
Good one, thanks!
I am actually working on some CMS templating for a flat HTML templating engine and making heavy use of the data attribute for it. I think it is one of the greatest additions to the HTML standard. I like that I did not have to use a cutom built templateing variable / languadge to indicate that a template should go into a node. It is my hope that a CMS standard will evolve out of data attributes so that we can get rid of the 1000's of different variations of a simple thing like put the content here.
The title is wrong, it basically says: don't store your data in class names and separate your styling from your code logic.
I guess this will be terribly slow in Internet Explorer.
It is, with IE9 handling just over 7,000 operations per second (based on the jsPerf test: http://jsperf.com/long-selectors-vs-data/2) vs. 25,000 in Chrome. Don't go all out I'd say, but in most scenario's even that number of operations will be acceptable.