How to include !important in jquery

Jquery

Jquery Problem Overview


I am trying to add !important in the css attribute using jQuery like

$("tabs").css('height','650px;!important');

but !important has no effect. How to include !important in jquery?

Jquery Solutions


Solution 1 - Jquery

Apparently it's possible to do this in jQuery:

$("#tabs").css("cssText", "height: 650px !important;");

Src: http://bugs.jquery.com/ticket/2066

Solution 2 - Jquery

I solved this problem with:

inputObj.css('cssText', inputObj.attr('style')+'padding-left: ' + (width + 5) + 'px !IMPORTANT;');

So no inline-Style is lost, an the last overrides the first

Solution 3 - Jquery

It is also possible to add more important properties:

inputObj.attr('style', 'color:black !important; background-color:#428bca !important;');

Solution 4 - Jquery

var tabsHeight = 650;
 
$("tabs").attr('style', 'height: '+ tabsHeight +'px !important');

OR

#CSS
.myclass{height:650px !important;}

then

$("tabs").addClass("myclass");

Solution 5 - Jquery

If you need to have jquery use !important for more than one item, this is how you would do it.

e.g. set an img tags max-width and max-height to 500px each

$('img').css('cssText', "max-width: 500px !important;' + "max-height: 500px !important;');

Solution 6 - Jquery

For those times when you need to use jquery to set !important properties, here is a plugin I build that will allow you to do so.

$.fn.important = function(key, value) {
    var q = Object.assign({}, this.style)
    q[key] = `${value} !important`;
    $(this).css("cssText", Object.entries(q).filter(x => x[1]).map(([k, v]) => (`${k}: ${v}`)).join(';'));
};

$('div').important('color', 'red');

Solution 7 - Jquery

If you really need to override css that has !important rules in it, for instance, in a case I ran into recently, overriding a wordpress theme required !important scss rules to break the theme, but since I was transpiling my code with webpack and (I assume this is why --)my css came along in the chain after the transpiled javascript, you can add a separate class rule in your stylesheet that overrides the first !important rule in the cascade, and toggle the heavier-weighted class rather than adjusting css dynamically. Just a thought.

Solution 8 - Jquery

You don't need !important when modifying CSS with jQuery since it modifies the style attribute on the elements in the DOM directly. !important is only needed in stylesheets to disallow a particular style rule from being overridden at a lower level. Modifying style directly is the lowest level you can go, so !important has no meaning.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSoftView Question on Stackoverflow
Solution 1 - JqueryklokopView Answer on Stackoverflow
Solution 2 - JqueryBurnerView Answer on Stackoverflow
Solution 3 - Jqueryuser3711819View Answer on Stackoverflow
Solution 4 - JqueryMurat KezliView Answer on Stackoverflow
Solution 5 - JqueryVincent TangView Answer on Stackoverflow
Solution 6 - JqueryBernestoView Answer on Stackoverflow
Solution 7 - JqueryWill MeierView Answer on Stackoverflow
Solution 8 - JqueryMarc WView Answer on Stackoverflow