I've got a reference to a CSSStyleRule
object in JavaScript and I want to update the style for border-top-color
to red !important
. If I assign the value red
, no problems occur. If I assign the value red !important
, the value is ignored (i.e. not assigned).
myStyleSheetRule.style.borderTopColor = 'red'; // success
myStyleSheetRule.style.borderTopColor = 'red !important'; // fail
How do I set the !important flag?
Note that it has to be done via a stylesheet rule accessed programatically. In my use case, I can't assign a style attribute or anything else. I'm using Chrome on Windows 7.
Something like this should work:
HTML
<div id="colored">?</div>
Default stylesheet
#colored {
border-top: 1px solid Black;
}
Javascript
var all = document.styleSheets,
s = all[all.length - 1],
l = s.cssRules.length;
if (s.insertRule) {
s.insertRule('#colored {border-top-color: Red !important}', l);
} else {
//IE
s.addRule('#colored', 'border-top-color: Red !important', -1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With