Find all elements with attribute on page

How to list all elements that have your chosen attribute in the browser console

Posted on Monday, 20 May 2019

You can list all elements on the page that have your selected attribute in the console. Inject the following code into the inspector console:

 1function getAllElementsWithAttribute(attribute) {
 2var matchingElements = [];
 3var allElements = document.getElementsByTagName('*')
 4for (var i = 0, n = allElements.length; i < n; i++) {
 5  if (allElements[i].getAttribute(attribute) !== null) {
 6    matchingElements.push(allElements[i]);
 7  }
 8}
 9return matchingElements;
10}
11getAllElementsWithAttribute('data-foo'); // Your value here

Source: Get elements by attribute when querySelectorAll is not available without using libraries?