0

I'm using the following CSS to hide my tag and show a tab #aside-expander to enable the user to view it if required when the browser width is less than 1200px.

@media screen and (max-width: 1200px) {
    aside#global-widget-base {
        display: none !important;
    }
    #aside-expander {
        display: inline !important;
    }
}

When using an click handler on the #aside-expander element in order to show the again, the element is not displayed. I assume this is due to the use of !important on the style element.

$('#aside-expander').click(function() {
   $("#global-widget-base").css("display", "inline"); 
});

I wondered if anybody was able to provide a solution as to how I can show this element on demand? I have already tried applying !important to my jQuery .css function to no avail.

Nick
  • 5,844
  • 11
  • 52
  • 98

1 Answers1

1

As AurelioDeRosa has stated if you are really needing !important you should probably look at a different way of implementing what you have — important should really only be reserved as a last resort, mainly because it is such a pain to override.

However, if you must, one way to get around this is to use classes as a switch from javascript, to turn states on or off ‐ for example:

@media screen and (max-width: 1200px) {
  aside#global-widget-base {
    display: none !important;
  }
  #aside-expander {
    display: inline !important;
  }
  aside#global-widget-base.hide-disabled {
    display: block !important;
  }
}

Then in your JavaScript you just need to add or remove the hide-disabled class. Obviously this can be tailored however you like, and depending on the call of the situation. I generally prefer to work upwards from the html element, so my JavaScript switches the "mode" of the page, but then this should only be used for an effect that is relevant to the entire page, and not something specific.

var states = 'full-collapse half-collapse no-collapse';

// remove all collapsing
$('html').removeClass(states).addClass('no-collapse');

// add collapse back
$('html').removeClass(states).addClass('full-collapse');
Pebbl
  • 34,937
  • 6
  • 62
  • 64