63

(This is using content scripts in a chrome extension)

I need to overwrite some css properties that the webpage has labeled as !important. Is this possible?

For instance, if I want to get rid of the border that is labeled important:

$(".someclass").css('border','none'); //does not work
3
  • 2
    Did you search SO? I found stackoverflow.com/questions/462537/… Commented Aug 15, 2012 at 1:29
  • @ŠimeVidas: That question is about Javascript; this question is about jQuery. The solution may be the same in the end, but the question's author wouldn't've known that (nor did I), and I think most devs would agree it's best practice to use a jQuery solution, when one exists. Commented Apr 21, 2015 at 22:30
  • Possible duplicate of How to apply !important using .css()? Commented Nov 19, 2018 at 15:49

6 Answers 6

130

Here you go:

$( '.someclass' ).each(function () {
    this.style.setProperty( 'border', 'none', 'important' );
});

Live demo: http://jsfiddle.net/Gtr54/

The .setProperty method of an element's style object enables you to pass a third argument which represents the priority. So, you're overriding an !important value with your own !important value. As far as I know, it is not possible to set the !important priority with jQuery, so your only option is the built-in .setProperty method.

Sign up to request clarification or add additional context in comments.

6 Comments

Fails in IE8 with "Object doesn't support this property or method"
@StuartDobson Yes, fails in IE8 -.-
Fails in Mosaic 2, too. :-P
It's a great answer with one caveat - if $('#container).style does not work - use $('#container)[0].style instead
@yaru Yes, the style property is only available on the DOM element itself, not on the jQuery object.
|
52

You can also do this:

$(".someclass").css("cssText", "border: none !important;");

1 Comment

this will overwrite the style
16

This should help.

$(".someclass").attr("style","border:none!important");

Updated, so as not to overwrite all styles:

var existingStyles = $(".someclass").attr("style");
$(".someclass").attr("style", existingStyles+"border:none!important");

1 Comment

Will replace all other styles
1

we can just add class using jquery

$("someclass").addClass("test");

<style>
.test{
border:none !important;
}
</style>

1 Comment

This won't work with a dynamically generated style (such as height or color). It's just ridiculous that jQuery devs are pitching this as a viable workaround. 😒
0

there is also another way

$("#m_divList tbody").find("tr[data-uid=" + row.uid + "]").find('td').css("cssText", "color: red !important;");

css("cssText", "color: red !important;");

Comments

-1

Use this below code to override the important in jQuery.

<style>  
  div.woocommerce-variation-add-to-cart-disabled {
    display: none ! important;
  }
</style>

<div class="woocommerce-variation-add-to-cart variations_button woocommerce-variation-add-to-cart-disabled">
  <script>
    jQuery(document).ready(function() {
      jQuery('.woocommerce-variation-add-to-cart').attr('style', 'display: block !important;');
    });
  </script>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.