29

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!

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
  • 1
    is using different classes possible so you can use toggleClass()? – Ohgodwhy Nov 07 '13 at 05:26
  • I think it's important to note that you're not changing the appearance of your page with jQuery, you're using jQuery as a tool to set CSS properties. This is why you have a number of bad answers. @danzkusuma's is the canonically-correct way to do what you want. – Jeremy Nov 07 '13 at 05:29
  • See also http://stackoverflow.com/questions/2655925/apply-important-css-style-using-jquery – Jeremy Nov 07 '13 at 05:41
  • 3 Working examples https://stackoverflow.com/questions/2655925/how-to-apply-important-using-css/44637199#44637199 – computerguy Jun 19 '17 at 19:14
  • 1
    I found this answer, is't perfect:
    https://stackoverflow.com/a/27066643/2269902
    – Hisham Dalal Apr 02 '18 at 09:50

15 Answers15

38

Try This :

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

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

Fendy Kusuma
  • 672
  • 7
  • 16
  • 6
    if there is only one item, just do : $('.myclass')[0].style.setProperty( 'height', '0px', 'important' ); – eosphere Sep 17 '14 at 13:44
  • Apparently this doesn't work if you are trying to overwrite overflow: something !important; – Frederico Schardong Jan 05 '16 at 15:48
  • 1
    Not working dear, getting error - "Uncaught TypeError: Cannot read property 'setProperty' of undefined". I wrote like this $('.location-info-field').style.setProperty( 'height', '20px', 'important' ); – Kamlesh Mar 06 '20 at 07:58
20

Another Easy method to solve this issue adding style attribute.

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

This will solve your problem easily... :)

user3556813
  • 213
  • 2
  • 3
  • 1
    if i use 'style' then existing other properties will be overridden right? and only single property will be applied! and if i do so..then i have to add all other properties...which is not handy..! So, it would be better to set a single 'property' rather than re-write whole 'style' attibute..! :) – SHEKHAR SHETE Nov 03 '14 at 05:06
  • This is not good solution, in case of already having other css on that same element. It is removing them and applying only width to 500px. Thanks. – Kamlesh Mar 06 '20 at 08:01
5

You can do this:

$(".myclass").css("cssText", "height: 0 !important;");

Using "cssText" as the property name and whatever you want added to the css as it's value.

9ete
  • 3,692
  • 1
  • 34
  • 32
3

inline style fails to override the !important given in stylesheets.

Therefore !important can be overridden only by using !important along jQuery

Try like

$('#divL3.myclass').attr('style', 'height: 0px !important');
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • 1
    This will wipe out all other inline styles. – Jeremy Nov 07 '13 at 05:30
  • @Jeremy No, I have mentioned the `id` selector(ids has to be unique). hope this gives you more understanding http://jsfiddle.net/Ezjc3/1/ – Praveen Nov 07 '13 at 05:33
  • 2
    no, sorry, my comment still applies. If you add `font-weight:bold` to the inline declaration, this jQuery will remove the `font-weight`. [example](http://jsfiddle.net/Ezjc3/2/) – Jeremy Nov 07 '13 at 05:35
2

This will work like a charm

    $( '#divL3' ).css("cssText", "height: 0px !important;")

here's the fiddle http://jsfiddle.net/abhiklpm/Z3WHM/2/

abhiklpm
  • 1,603
  • 2
  • 16
  • 21
1
$('#divL3.myclass').attr('style', 'height:0px !important');

Would something like the above work?

TGH
  • 38,769
  • 12
  • 102
  • 135
  • 1
    This will wipe out all other inline styles, which is bad. – Jeremy Nov 07 '13 at 05:31
  • @Jeremy I agree, if there are any. However, if everything else is using classes than this could be an acceptable one off used as a workaround. Ideally it would be better to fix this by applying another class. Important should be used sparingly. In the above example there are no inline styles by default – TGH Nov 07 '13 at 05:33
1

There is another trick to get it done!!! Can you remove the myclass from the element and apply the height. Like

$('#divL3.myclass').removeClass('myclass').css('height', '0px');
Ayyappan Sekar
  • 11,007
  • 2
  • 18
  • 22
1

First Id is unique identifier, therefore you don't need to search div by it's class name. therefore you can filter div by it's id.

Try following code

first add new Class property below myClass like this

<style>
            .myclass{
                height:50px !important;
                border: 1px solid red;
            }
            .testClass{
                height: 0 !important;
            }
        </style>

it will override height property of myClass

now just add this class to div

$('#divL3').addClass('testClass');

but making height "0" wont hide content of "divL3". you rather try to hide overflow content or hide entire div by adding css property display:none

hope it will work for you.

Zainul Abdin
  • 835
  • 4
  • 10
1

no use.suppose if i have some css properties in style attribure.it won't work.so please use below code

 var styleAttr = $("#divAddDetailPans").attr('style');
        $("#divAddDetailPans").removeAttr('style');
        $("#divAddDetailPans").attr('style', styleAttr.replace('top: 198px !important', 'top: 78px !important'));
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
mahendran
  • 11
  • 1
1

I prefer to create a class and add it to specific div, for example

This can be either inline or in an external CSS file:

<style>
.class-with-important { height: 0px !important; }
</style>

If this is something that is never going to change, you can manually add the class to that div:

<div id="divL3" class="myclass class-with-important">sometext here</div>

or you can use jQuery to add this script tag at the bottom of that page

<script>
 $(document).ready(function(){
   $('#divL3').addClass("class-with-important");
 });
</script>

You can use the 'style' option, but would recommend doing it this way:

 var currentStyle = $('#divL3').attr('style');
 $('#divL3').attr('style', currentStyle + ' width:500px !important;');

This way you do not override the existing style, in case a third party plugin adds any dynamic style to your page, which is not unusual if you are using a CMS like WordPress.

hatsrumandcode
  • 1,871
  • 2
  • 19
  • 21
0

Can you try this method,

        <style type="text/css">
        .myclass{
             height:50px !important;
        }
        .newclass{
             height:0px;
        }
        </style>

Jquery:

  $('#divL3').removeClass('myclass').addClass('newclass');  
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • hey @Krish R, what about the events that are wrote for .myclass? it will be scrapped if i do change the class..! there is a dependenacy as these divs & its following divs show content depending on classes that we applied..! – SHEKHAR SHETE Nov 03 '14 at 05:12
0

I dont understand why you have !important in your CSS if you hope to override it later. Remove the !important in your CSS height. CSS always prioritizes your HTML tag styles over CSS.

 .myclass{
   height:50px;
  }

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

This is better for design.

F.O.O
  • 4,730
  • 4
  • 24
  • 34
0

I had the same issue, so I set up max-height in css, and then overrided height by jQuery:

css

#elem{
  max-height:30px !important;
}

jQuery:

$('#elem').height('30px !important');
Angel M.
  • 2,692
  • 2
  • 32
  • 43
0

I was running into a problem where we allow the users to define their own font size for Kendo Grids, however after setting the value, and then manipulating the grid in any way, it'd reset the font size. So we actually defined an ID for the style tag and changed the html with the proper CSS each time.

At the top of our main page, we set the font-size by what is saved in their PHP Session Variable, or from the database.

echo "<style id='grid-style'>table { font-size: $Grid_Font_Size !important; }</style>";

Then in order to change it, we do the following jquery where font_size is the new size

$( "#grid-style" ).html( "table { font-size: " + font_size + "px !important; }" );
Jeff Beagley
  • 945
  • 9
  • 19
-5

Have you tried this?

$('#divL3.myclass').css('height', '0px !important');
legendofawesomeness
  • 2,901
  • 2
  • 19
  • 32