4 ms·
Don't use class names to find HTML elements with JS
- kls 15y agoI 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.
- unfletch 15y agoThe 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.
- roytomeij 15y agoGood one, thanks!
- arvidj 15y agoI guess this will be terribly slow in Internet Explorer.
- roytomeij 15y agoIt is, with IE9 handling just over 7,000 operations per second (based on the jsPerf test: http://jsperf.com/long-selectors-vs-data/2 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.
- voidr 15y agoThe title is wrong, it basically says: don't store your data in class names and separate your styling from your code logic.