0

I want to be able to remove/change the internal style sheets values through JavasScript. It has to be through JS because I cannot edit the html since I am using an application that does not allow me to, but I can use JS. How can I change the value for the background or remove it completely? Or is there another way to accomplish this?

<body>
<head>
  <style type="text/css">
  #company-header{background:#000 !important;}
  </style>
</head>

<div id="company-header"></div>
</body>
2
  • Do you have jQuery referenced from the page already? I am asking because if you cannot the html, how would you add any jQuery or JavaScript code? Commented Oct 2, 2011 at 0:21
  • Jquery is already used on the site. In the back end of the site are editable regions that could include JS, but are typlically only used to add html. Commented Oct 2, 2011 at 0:31

3 Answers 3

1

If your just going to change small bits of css, using jQuery's css() is your best option, but it does not always recognize !important, for that you would probably need cssText, like this:

$('#id').css('cssText', 'height: 200px !important');

This replaces all the css, so everything needs to be defined in the new css rules that are added.

If you are changing a lot of css, or just want to make it easier for the next time, you could remove all inline css and add an external stylesheet instead.

To remove all inline styles you would do something like this:

$(document).removeAttr('style');

or for div's only

$('div').removeAttr('style');

Depending on how many styles there are, this could take som time to process.

Then to add a new stylesheet do:

(function() {
    var myCSS = document.createElement('link');
    myCSS.rel = 'stylesheet';
    myCSS.type = 'text/css';
    myCSS.src = '/styles/mystylesheet.css';
    var place = document.getElementsByTagName('script')[0];
    place.parentNode.insertBefore(myCSS, place);
})();

Edit:

function removeCSS() {
    var badCSS = document.getElementsByTagName('style')[0];
        $(badCSS).remove();     
});

This will remove all the markup in the internal stylesheet, but since the styles are already loaded it will make absolutely no difference.

Internal styles will always override external styles, but for one exeption, if the external style is !important. If both the external and internal styles are !important, the internal style will be used.

Loading an external stylesheet dynamicly with javascript will only work if everything you are trying to override is set to !important in the external stylesheet, and not set to !important in the internal stylesheet.

Changing the styles directly in the DOM with jQuery's css() and using the cssText option to override the !important set in the internal stylesheet may be your only viable option if there is absolutely no way to alter the html file and remove the internal stylesheet.

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

4 Comments

Thank you for you're reply. Removing the styles would be great, but they are not inline. They are in an internal style sheet. How can I target them?
@kfawcett: You should load in your own custom stylesheet (or make your own style element), then override whatever has been defined in your "internal stylesheet", and be sure to include !important. That is the only way to do this. Forget any ideas about "removing the styles". If my comment doesn't make sense to you, let me know and I'll write a complete answer.
@adeneo This works, but how would I add more than one attribute? I want to change the background and padding and border. $('#id').css('cssText', 'height: 200px !important');
@adeneo Thank you! This led me in the right direction and worked. $('#id').css('cssText', 'height: 200px !important'); I eventually found this to add multiple properties. stoimen.com/blog/2010/03/17/…
0

EDIT: OK, now that we understand that this question is really just about how to override the !important style declaration when setting styles via javascript, this is a duplicate of this posting: Overriding !important style.

The two possible answers there are to set a whole style string on the object or to create a new stylesheet that refers to this object.


Previous answer before question was edited:

You can just set some style directly on the object if you want like this. This will override anything that comes from a style sheet so you don't have to mess with the style sheet.

$("#company-header").css("background-color", "#FFFFFF");

or

$("#company-header").css("background", "none");

A more extensible way of modifying the look of an object or groups of objects is to based the style settings on class names and then add/remove class names using jQuery. This has the effect of switching which style sheet rules apply to a given object without having to directly manipulate the style sheets themselves:

$("#company-header").removeClass("oldClass").addClass("newClass");

8 Comments

That's exactly what I tried to do, but for some reason IE6,7,8 and all Firefox's do not recognize .css(). It worked in Chrome, Safari, and Opera. And using commas would not work. I had to use colons and specify !important due to the internal styles already being set with !important. $("#company-header").css({'background': 'none !important'});
You neglected to include the info about !important in your first post (I see you've edited it now). So, is your problem solved now?
No it's still not solved. I put the !important in there to further elaborate what I am trying to solve. The CSS in the internal style sheet has !important in it, so I added !important to the JS too. That is what works in Chrome, Opera, etc., but not in IE and Firefox.
.css is a jQuery method and jQuery is totally cross-browsr compatible, so just make sure you've included the jQuery file on your page
in fact, the .css method doesn't have anything to do with the external style sheet, it only changes the css that is applied to the DOM object. (if you have !important in your external style sheet for something you want to change, remove it, because that might be giving the external style sheets properties a higher priority than the script)
|
0
$(document).ready(function(){
        $('style').remove();
    });

This code will remove the all internal css from the site. I used this but style tag will be visible in the page source.

Comments

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.