solvit.io

Select all the elements with a data-* HTML attribute using JavaScript

Solution
// find elements that have the 'data-xxx' attribute with any value
// (it also selects elements with no value for this attribute)
var elements = document.querySelectorAll('[data-xxx]');

// find elements that have the 'data-xxx' attribute with the value 'yyy'
var elements = document.querySelectorAll('[data-xxx="yyy"]');
Example
var elements = document.querySelectorAll('[data-toggle]');
var elements = document.querySelectorAll('[data-target="#main-menu"]');
Find more solutions about: ,