Skip to content

Instantly share code, notes, and snippets.

@techniq
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techniq/82036ef13598396e0d9b to your computer and use it in GitHub Desktop.
Save techniq/82036ef13598396e0d9b to your computer and use it in GitHub Desktop.
Find all elements with a specific style attribute
getElementsWithStyle('border.*radius');
getElementsWithStyle('box-shadow');
// Array.from polyfill
if (!Array.from) {
Array.from = function (object) {
'use strict';
return [].slice.call(object);
};
}
var styleSheets = Array.from(document.styleSheets);
function getElementsWithStyle(styleName) {
return styleSheets.reduce(function(results, styleSheet) {
if (styleSheet.rules) {
var rules = Array.from(styleSheet.rules);
rules.forEach(function(rule) {
if (rule.type === CSSRule.STYLE_RULE) {
var styleDeclarations = Array.from(rule.style);
styleDeclarations.forEach(function(styleDeclaration) {
if (new RegExp(styleName).test(styleDeclaration)) {
if (!(rule.selectorText in results)) {
// Only add rule once for each selector for (ex. border-.*-radius => border-top-left-radius, border-top-right-radius, etc)
results[rule.selectorText] = Array.from(document.querySelectorAll(rule.selectorText))
}
}
});
}
})
}
return results;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment