Skip to content

Instantly share code, notes, and snippets.

@boblauer
Last active December 22, 2015 11:59
Show Gist options
  • Save boblauer/6469430 to your computer and use it in GitHub Desktop.
Save boblauer/6469430 to your computer and use it in GitHub Desktop.
Check if a class is affecting the style of an element.
(function() {
var affected = [], unaffected = [],
className;
className = prompt('Class to check:');
if (!className) return;
$('.' + className).each(function() {
var element = this,
$element = $(this),
before = [], after = [];
var styleProps = Object.keys(JSON.parse(JSON.stringify(element.style)));
var before = [], after = [];
styleProps.forEach(function(style) {
before.push($element.css(style));
});
$element.removeClass(className);
styleProps.forEach(function(style) {
after.push($element.css(style));
});
$element.addClass(className);
if (before.join('') === after.join('')) {
unaffected.push(element);
}
else {
affected.push(element);
}
});
console.log(affected.length + ' element(s) DO have styles applied from the ' + className + ' class.');
affected.forEach(function(el) { console.log(el); });
console.log(unaffected.length + ' element(s) do NOT have styles applied from the ' + className + ' class.');
unaffected.forEach(function(el) { console.log(el); });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment