I have been working on an application, the front-end is primarily using jQuery.
We rely on certain classed elements being present on the page so that we can attach behaviour to them. For example:
$('.block').on('click', clickHandler);
One of the other developers said we ought to decouple presentation from logic (which I agree with). Because the classes are used for presentation, he suggested using data attributes:
$('[data-attribute-name~=value]').on('click', clickHandler);
However, I know the following about this approach:
- It is significantly less performant than a class-based selector
- HTML classes are used to impart semantic meaning to a DOM element, and, as such, are not restricted to presentational uses.
I don't see any particular mention of either when reading up on unobtrusive javascript.
What are the major differences of using [data-attribute]
over classes / IDs?
Is it strictly a matter of performance / preference?