3

Let's say I have a div.foo with this CSS:

.foo {
    height: 70vh;
}

And I change the height of div.foo with jQuery like this:

$('.foo').height(100);

How do I reset the CSS of div.foo so its height is rendered 70vh again?

One solution that works is to reload the entire CSS file this way. But I wonder if there is a solution to only reset the CSS of div.foo, not the entire stylesheet?

Community
  • 1
  • 1
Rotareti
  • 49,483
  • 23
  • 112
  • 108

3 Answers3

3

With jQuery, you are adding your styles to the style attribute of the element. It is removed again, if you call the function again with an empty string (""). This will remove the property in the style attribute.

If you have e.g. this command:

$('.foo').css('height', '100px');

You can revoke it by

$('.foo').css('height', '');

See also the discussion here: https://stackoverflow.com/a/4036868/3233827

Community
  • 1
  • 1
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
3

$('.foo).height(''); should be sufficient to erase the style applied by jQuery

Quentin Roy
  • 7,677
  • 2
  • 32
  • 50
1

You can remove the style attribute from the element. This attribute holds any dynamic styles.

$('.foo').css('background', 'teal');

$('#reset').click(function() {
    $('.foo').removeAttr('style');
});
.foo {
    height:100px;
    width:100px;
    background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>

<div class="foo"></div>
<p><button id="reset">Reset style</button></p>
Midas
  • 7,012
  • 5
  • 34
  • 52