tags:

views:

347

answers:

4

I am having trouble applying a style that is !important. Ive tried:

$("#elem").css("width", "100px !important");

This does nothing, no width style whatsoever is applied. Is there a jquery-ish way of applying such a style without having to overwrite cssText (which would mean id need to parse it first etc)

Edit: i should add that i have a stylesheet with an !important style that i am trying to override with an !important style inline, so using .width() and the like does not work since it gets overrridden by my external !important style.

Also, the value that will override the previous thing is computed, so i cannot simply create another external style.

+3  A: 

You can set the width directly using .width() like this:

$("#elem").width(100);

Updated for comments: You have this option as well, but it'll replace all css on the element, so not sure it's any more viable:

$('#elem').css('cssText', 'width: 100px !important');
Nick Craver
ok, i used with as an example, what i care about is setting !important.
mkoryak
also i edited the question to reflect my situation better.. basically i have an external !important width that is set to something bad that i must override inline. width() does not work for this reason
mkoryak
@mkoryak - Updated with the only other non-class option, not sure if it'll fit your situation or not.
Nick Craver
+3  A: 

The problem is caused by jQuery not understanding the !important attribute, and as such fails to apply the rule.

You might be able to work around that problem, and apply the rule by referring to it, via addClass():

.importantRule { width: 100px !important; }

$('#elem').addClass('importantRule');

Or by using attr():

$('#elem').attr('style', '"width: 100px !important"');

The latter approach would unset any previously set in-line style rules, though. So use with care.

Of course, there's a good argument that @Nick Craver's method is easier/wiser.

David Thomas
i am leaning toward your latter approach, but what is sad about it is that ill probably end up having to parse the previous cssText because i cant just discard it
mkoryak
FYI, In your first snippet you should change .rule to .importantRule or the other way around.
Sinan Y.
@Sinan Y. you're absolutely right...grr, *typos* *edited and changed.* Thanks! =)
David Thomas
@ricebowl np, i made a quick test, what you're saying unfortunately won't work in this example. Browsers put a higher priority to styles which are defined to an ID rather than a class. see. http://sinan.kodingen.com/csstest.html
Sinan Y.
@Sinan Y., I was rather foolishly, and optimistically, hoping that there wasn't a contradictory rule set in the ID-related rules. I was also hoping, to be entirely honest, that `!important` would override those that were (unless they were also specified with `!important`). Still, at least my final '@Nick Craver's method is easier/wiser' holds true! ^.^
David Thomas
btw Nick Craver's method is no different than yours...:) just another way of writing it with jquery... changing style attribute or changing cssText with jquery's css method does exactly the same thing.
Sinan Y.
@Sinan Y., yeah, I know; I was just being all self-deprecating and English about it... =b
David Thomas
ah, sorry couldn't get it, sometimes English humour goes beyond my understanding...:)
Sinan Y.
A: 

I would assume you tried it without adding important?
inline css (which is how js adds styling) overrides stylesheet css. I'm pretty sure that's the case even when the stylesheet css rule has !important.

Another question (maybe a stupid question but must be asked.): is the element you are trying to work on, is it display:block; or display:inline-block; ?

not knowing your expertise in CSS.. inline elements don't always behave as you would expect.

css rules that have !important override everything, no matter where they are located, only exception being that inline styles with !important will override stylesheet styles with !important. This is the problem i am having. there is a stylesheet rule with !important that i would like to override. whats more, the value i need to supply must be computed through JS, so i cant simply change the stylesheet itself.
mkoryak
+1  A: 

If it is not so relevant and since you're dealing with one element which is #elem, you can change its id to something else and style it as you wish...

$('#elem').attr('id','cheaterId');

and in your css:

#cheaterId { width: 100px;}

hope this helps, Sinan.

Sinan Y.
+1 for the absolute simplicity.
David Thomas