views:

3518

answers:

8

Title pretty much sums it up.

The external style sheet has the following code:

td.EvenRow a{
  display: none !important;
}

I have tried using:

element.style.display = "inline";

and

element.style.display = "inline !important";

but neither works. Is it possible to override an !important style using javascript.

This is for a greasemonkey extension, if that makes a difference.

Much appreciated, Enrico

+10  A: 

I believe the only way to do this it to add the style as a new CSS declaration with the '!important' suffix. The easiest way to do this is to append a new <style> element to the head of document:

function addNewStyle(newStyle) {
    var styleElement = document.getElementById('styles_js');
    if (!styleElement) {
        styleElement = document.createElement('style');
        styleElement.type = 'text/css';
        styleElement.id = 'styles_js';
        document.getElementsByTagName('head')[0].appendChild(styleElement);
    }
    styleElement.appendChild(document.createTextNode(newStyle));
}

addNewStyle('td.EvenRow a {display:inline !important;}')

The rules added with the above method will (if you use the !important suffix) override other previously set styling. If you're not using the suffix then make sure to take concepts like 'specificity' into account.

J-P
This could be replaced by a one-liner. See below: http://stackoverflow.com/questions/462537/overriding-important-style-using-javascript/1577204#1577204
Premasagar
+1  A: 

You can nuke the class, but this might have other side effects you don't want.

element.className = null; // or your favorite replacement class
Ishmael
+3  A: 

element.style has a setProperty method that can take the priority as a third parameter:

element.style.setProperty("display", "inline", "important")

It might well be something specific to Firefox but it should be fine for your greasemonkey script.

sth
+3  A: 

There's are a couple of simple one-liners you can use to do this.

1) Set a a "style" attribute on the element:

element.setAttribute('style', 'display:inline !important');

or...

2) Modify the cssText property of the style object:

element.style.cssText = 'display:inline !important';

Either will do the job.

===

BTW - if you want a useful tool to manipulate !important rules in elements, I've written a jQuery plugin called "important": http://github.com/premasagar/important

Premasagar
A: 

Premasagar, works perfect! Thanks!

Bart
A: 

Thanks for the tip. I override color: !important with another class with color: !important.

A: 

If all you are doing is adding css to the page, then I would suggest you use the Stylish addon, and write a user style instead of a user script, because a user style is more efficient and appropriate.

See this page with information on how to create a user style

Erik Vold
Modifying the css is only a part of what my gm extension does
Enrico
A: 

Better method I just discovered: try to be more specific than the page CSS with your selectors. I just had to do this today, and it works like a charm! More info on the W3C site: http://www.w3.org/TR/CSS2/cascade.html#specificity

Nuck