Skip to content

Instantly share code, notes, and snippets.

@AleksueiR
Forked from anonymous/importantStyle.js
Created May 30, 2014 15:22
Show Gist options
  • Save AleksueiR/1368f925f63fab665a0d to your computer and use it in GitHub Desktop.
Save AleksueiR/1368f925f63fab665a0d to your computer and use it in GitHub Desktop.
(function () {
var elCache = [];
var styleCache = [];
forceStyle = function (el, obj) {
if (elCache.indexOf(el) == -1) {
elCache.push(el);
styleCache.push(el.style.cssText);
}
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
el.style.cssText += ';' + prop + ':' + obj[prop] + ' !important';
}
};
};
restoreStyle = function (el) {
var index = elCache.indexOf(el);
if (index == -1) {
return;
}
el.style.cssText = styleCache[index];
elCache.splice(index, 1);
styleCache.splice(index, 1);
};
})();
// Example usage:
forceStyle(document.body, {margin: '10px', 'border-left': '2px solid red'});
restoreStyle(document.body);
@CollinChaffin
Copy link

Did you ever get this working considering the usage string is totally a joke and isn't even consistent in the usage of quoting or the tokens? I struggled to even get it working it sure looks like a quickly thrown together wireframe of a function that was never tested but I really liked the idea so was hoping someone had gotten it working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment