1

Suppose I have the following css styles

.someclass{
background: red !important;
}
.someclass{
background: blue !important;
}

Now with jquery is it possible to remove lastly defined style in the stylesheet so that who ever write .someclass in the new line that background can't be changed.

4
  • Why you want to do that with jQuery which you can do it with CSS only? - @C-Link Commented Nov 28, 2013 at 9:21
  • If I do it with jquery then afterwards if anyone change the css that can't be changed. Commented Nov 28, 2013 at 9:23
  • 2
    I am not getting your purpose as to why you want to do this, but this is simply creating an unnecessary workaround. - @C-Link Commented Nov 28, 2013 at 9:25
  • Why not just change the order you include the stylesheets? Split the hard-coded, most important one into a separate file from the one that people can change, then simply include your important one after the changeable one Commented Nov 28, 2013 at 9:25

3 Answers 3

2

No but you can add a new style which will override the previous one.

$('.someclass').css("cssText", "background: red !important;");

Since you have declared important in your css, you will need to pass over this argument when setting it in your jquery.

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

what's the cssText? I've never seen like that.
It replaces the inline style tag and in this case allows you to pass over a third arguement. Its not well documented, I stumbled upon it on a project not long ago.
0

You can remove the current class and add a new one with the whatever style you want.

$( identifier ).removeClass("someclass");
$( identifier ).addClass("newclass");

And in css file you can add:

.newclass{
  background: red !important;
 }

I hope this it will be usefull to you

1 Comment

This is not what he asked.
0

You can write $('.somebody').css('background','red') in Js file.

1 Comment

This wont override the !important which is set in the CSS. See my answer which passes over the !important argument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.