jQuery CSS !important
One thing that everyone must have wonder (those that haven’t tried yet) is whether jQuery support declaration of !important for their css() method such as this.
jQuery(‘#id’).css(‘height’, ‘200px !important’);
document.getElementById(‘id’).style.height = “200px !important”;
Solution to jQuery CSS !important
Well, the typical way of getting !important will not work. Rather, you might want to try CSS DOM cssText. Hence, you will force !important to your jQuery statement by doing this.
jQuery(‘#id’).css(‘cssText’, ‘height: 200px !important’);
document.getElementById(‘id’).style.cssText = ‘height: 200px !important’
While normally you will use it this way.
document.getElementById(‘id’).cssText = ‘height: 200px !important’
Definitely, there is a better alternative such as using the cssRules/rules array which can specify the rule in that element to be modify.
The other more practical way of doing it is by introducing new class for your element. Instead of styling through jQuery or DOM object which is applying inline styling (means highest priority already), you should leave every majority styling or best all of the styling to an external style sheet that help you reduce some bytes on the script and also improve maintainability for both your code and style. You can do this using the toggleClass which adds a new class to that element automatically.
document.getElementById(‘id’).className = ‘classname’;
Using !important in CSS is not advisable since it might kill your web usability. Furthermore, there is no reason why !important should be use as inline styling already has the highest priority. Hence, you might want to reconsider applying !important on your script after thinking about the consequences that might bought to your users.