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
    – Nitesh
    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
    – Nitesh
    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
    – Joe
    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

2
  • what's the cssText? I've never seen like that. Commented Nov 28, 2013 at 9:39
  • 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.
    – heymega
    Commented Nov 28, 2013 at 9:42
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
  • This is not what he asked.
    – grimmus
    Commented Nov 28, 2013 at 9:30
0

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

1
  • This wont override the !important which is set in the CSS. See my answer which passes over the !important argument
    – heymega
    Commented Nov 28, 2013 at 9:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.