I want to pass the following variable like so:
var $headerHeight = jQuery("#header").outerHeight() - jQuery("#header .main-menu").outerHeight();
jQuery(this).find('nav').attr('style', 'margin-top:' + $headerHeight + '!important');
It's obviously used in the wrong syntax; I just can't figure out how to use it correctly. The jQuery ".css" function isn't an option because it has to have the "!important" declaration. How can I pass $headerHeight in the above example?
KatieK
13.9k19 gold badges79 silver badges91 bronze badges
3 Answers 3
var headerHeight = $("#header").outerHeight() - $("#header .main-menu").outerHeight();
jQuery(this).find('nav').attr('style', 'margin-top:' +headerHeight+'px ' + '!important');
answered Dec 3, 2012 at 19:50
Scorpio
1,1711 gold badge21 silver badges38 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You need to add a unit to the property value. (probably px)
answered Dec 3, 2012 at 19:39
SLaks
891k182 gold badges1.9k silver badges2k bronze badges
1 Comment
Tom
Thanks for this, if you're interested in helping a dude out see the comment above, if not cheers anyways! Tom.
You're fixing the wrong problem. Instead of un-doing a change that slideToggle does, simply don't use slideToggle.
$(myelement).animate({
height: "200px" // or whatever your target height is, calculated or static.
},500/*duration*/);
answered Dec 3, 2012 at 20:01
user400654
95.1k16 gold badges168 silver badges188 bronze badges
Comments
lang-js
!importanton an inline-style, inline styles override stylesheets unless the declaration is using!important. If that's the case, the correct fix would be to fix the css.+'px!important', however I do tend to agree, having to use important this way is a sign that you're doing something wrong.$.animate, it will result in much cleaner code than using slideDown with inline!important. slideDown's css changes will override your!importantanyway.