Select elements by HTML5 data attribute in jQuery

You can select on a data- attribute like any other attribute…using an attribute selector. In this case you want the attribute-equals selector, like this:

$("div[data-role="footer"]")

They are handled specially in consumption by jQuery, e.g. allowing .data() to fetch from them with correct typing…but as far as DOM traversal goes, they’re just another attribute, so think of them as such when writing selectors.

Leave a Comment

tech