Frage

we are all familiar with getElementByID, getElementsByClassName, and document.querySelector() methods. My question would be, as HTML5 allows us to use data-set now, can we query elements by their data-set? Thanks.

War es hilfreich?

Lösung 2

@dystroy's answer is probably exactly what you need. Additionally, query all the data-set might not be supported by all browsers. Instead, I create a class name that is similar (or identical) to the data-set name and use that as the anchor to query the elements.

Andere Tipps

There's not a lot you can do but you can get the elements having a specific variable in dataset using :

var elements = document.querySelectorAll('[data-name]');

and you can get elements having a specific value using

var elements = document.querySelectorAll('[data-name="value"]');
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top