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>
kfawcett
  • 73
  • 1
  • 4
  • 10
  • 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? – Hari Pachuveetil Oct 02 '11 at 00: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. – kfawcett Oct 02 '11 at 00:31

3 Answers3

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.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • 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 Oct 02 '11 at 03:25
  • @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. – thirtydot Oct 02 '11 at 03:59
  • @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'); – kfawcett Oct 02 '11 at 17:29
  • @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. http://www.stoimen.com/blog/2010/03/17/jquery-csstext-helps-you-improve-browser-reflows/ – kfawcett Oct 02 '11 at 17:44
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.

Harman Cheema
  • 152
  • 1
  • 3
  • 11
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");
Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 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'}); – kfawcett Oct 02 '11 at 00:36
  • 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? – jfriend00 Oct 02 '11 at 00:59
  • 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. – kfawcett Oct 02 '11 at 01:26
  • .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 – Mark Kramer Oct 02 '11 at 01:26
  • 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) – Mark Kramer Oct 02 '11 at 01:30
  • @MarkKramer This is an INTERNAL style sheet that is uneditable by me. That is why I am trying to target stuff with JavaScript. – kfawcett Oct 02 '11 at 02:11
  • Same thing, everything I said still applies – Mark Kramer Oct 02 '11 at 03:04
  • This question now looks to me like a dup of this: http://stackoverflow.com/questions/462537/overriding-important-style-using-javascript which I've now added a reference to in my answer. – jfriend00 Oct 02 '11 at 07:56