Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override css containing '!important' using jquery?

Tags:

jquery

css

I want to apply height for specific div using jquery but not able to override the '!important' keyword using jquery.

Here is markup:

<div id="divL1" class="myclass">sometext here</div> <div id="divL2" class="myclass">sometext here</div> <div id="divL3" class="myclass">sometext here</div> <div id="divL4" class="myclass">sometext here</div> 

css:

.myclass{  height:50px !important; } 

Here i want to find the class "myclass" where divid equals "divL3"

I have Tried with:

$('#divL3.myclass').css('height', '0px'); 

but wont work! so, how to override '!important' css using jquery.

Help appreciated!

like image 412
SHEKHAR SHETE Avatar asked Nov 07 '13 05:11

SHEKHAR SHETE


People also ask

Can jQuery override CSS?

cssHooks. Hook directly into jQuery to override how particular CSS properties are retrieved or set, normalize CSS property naming, or create custom properties.

How can we make CSS property important in jQuery?

You can do this: $("#elem"). css("cssText", "width: 100px ! important;");

Can jQuery manipulate CSS?

The jQuery CSS methods allow you to manipulate CSS class or style properties of DOM elements. Use the selector to get the reference of an element(s) and then call jQuery css methods to edit it. Important DOM manipulation methods: css(), addClass(), hasClass(), removeClass(), toggleClass() etc.


2 Answers

Try This :

$( '.myclass' ).each(function () {     this.style.setProperty( 'height', '0px', 'important' ); }); 

.setProperty () enables you to pass a third argument which represents the priority.

like image 187
Fendy Kusuma Avatar answered Sep 20 '22 03:09

Fendy Kusuma


Another Easy method to solve this issue adding style attribute.

$('.selector').attr('style','width:500px !important'); 

This will solve your problem easily... :)

like image 26
user3556813 Avatar answered Sep 19 '22 03:09

user3556813