2

I have a CSS property that was set in a stylesheet file:

.main-outer-div-filter {
    max-height: 230px;
}

I am trying to remove it with jQuery like so:

$('.main-outer-div-filter').css('max-height','');

For some reason, this way doesn't seem to have any affect, it only works if I set the style inline, but not when it is in the CSS file. Is there any way to use jQuery to reset/remove CSS properties, which were set in a CSS file?

Thank you

6
  • Use classes instead of that, you can't reset CSS proprieties to initial values Commented Jan 21, 2016 at 7:41
  • you can overwrite css from jQuery with !important Commented Jan 21, 2016 at 7:45
  • 1
    jQuery's .css() method adds inline style to the matched elements. Since the specificity value of inline style is higher than any CSS selectors, it should get applied actually. I suggest you have a look to your web browser devtools to figure it out. As an aside, in the context of CSS it is called a property, not an attribute. Commented Jan 21, 2016 at 7:47
  • 1
    By any chance, if you want to modify the external stylesheet in DOM, you can use document.styleSheets object (along with its methods). But jQuery doesn't provide any tools for that. Therefore you should do it with pure JavaScript. Commented Jan 21, 2016 at 8:06
  • 1
    @CreativeMind I think it is already answered on S.O. You might want to have a look at: Can jQuery change css style definition topic. You could also find some examples here. Commented Jan 21, 2016 at 8:24

3 Answers 3

4

With ('max-height', '') you are removing inline css. That would work if you set it inline before. To reset it, use initial value:

$(document).ready(function() {
  $('.special').css({
    'max-height': 'initial'
  });
});
li {
  height: 50px;
  border: 1px solid black;
  max-height: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>Max-height: 20px;</li>
  <li class="special">Max-height: initial;</li>
</ul>

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

Comments

1

You can overwrite a property with inline style using jQuery, as you are doing now, only you need to set this property to something. If you have a max-height set to 230px and want it to reset completely, then use 'max-height', 'none'

Comments

1

I would suggest you to use new class name to override it:

.main-outer-div-filter {
    max-height: 230px;
}
.main-outer-div-filter.override {
    max-height: initial; /* or none; */
}

and in the js you can set it with:

$('.main-outer-div-filter').addClass('override');

2 Comments

yes, he could use new classes or inline CSS, as I believe he knows, but that's not the question
@AnnieTrubak then the answer is very short: No!! can't be done.

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.