Here I am needing a little help to get a broader view on this.
As a rule I usually avoid using class
to query elements in HTML, for this I end up using a custom data property
.
In many cases I end up having duplicates in html:
<div data-content="" class="content" />
Where my CSS would look something like this:
.content { ... }
And to query this element in JS would do something like this:
var content = document.querySelectorAll("[data-content]");
My intention here is to keep the presentation layer separate from the behavior so that a change in page layout does not affect scripts and vice versa.
So I would like to hear the strengths and weaknesses of this approach? I even accept a JSPerf comparing querySelectorAll
to getElementsByNameClass