1

How would you remove 00bf00 part from this:

#main { background:#00bf00 00bf00 url('images/low.png') repeat;  }  

I need to do this with dynamically, so probably javascript ( or something else if you have idea)

I don't want to change code to output properly, just to see if there is a way to remove it by adding code snippet

3
  • I wouldn't want to bet on browser error recovery not having discarded the rule already. I'd really look at fixing it before it gets delivered to the client. Commented Jun 9, 2012 at 22:17
  • yeah you are right, I will fix this to show properly Commented Jun 9, 2012 at 22:20
  • @ZarkoDjuric What way are you used to load this style? link tag or style tag? Commented Jun 9, 2012 at 23:17

4 Answers 4

2
// for imported stylesheets
var cssRules = [];
for (var i = 0, il = document.styleSheets.length; i < il; i++) {
    if (document.styleSheets[0].cssRules)
        cssRules = document.styleSheets[0].cssRules;
    else if (document.styleSheets[0].rules)
        cssRules = document.styleSheets[0].rules​;
    for (var j = 0, jl = cssRules.length; j < jl; j++) {
        if (/^#main/.test(cssRules[j].cssText))
            cssRules[j].cssText = cssRules[j].cssText.replace(/[^#]00bf00/, '');
    }
}

// for style tags
​var styleTags = document.getElementsByTagName('style');
for (var i = 0, il = styleTags.length; i < il; i++) {
    styleTags[i].innerHTML = styleTags[i].innerHTML
                                         .replace(/(#main.*)[^#]00bf00/, '$1');
}​​

See demo (for style tags)

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

Comments

0

you cannot modify css properties directly within the head tag, try this:

$('#main').css('background-color', "transparent");

or:

document.getElementById('main').style.backgroundColor = 'transparent';

6 Comments

won't help, this needs higher lower level of javascript probably
None is actually not a valid value for background-color. Use "transparent" instead.
"you cannot modify css properties directly within the head tag" — Yes, you can.
The question isn't about setting the background colour to transparent, it is about removing the invalid section from the rule. i.e. the bold bit here: #00bf00 00bf00 url
@Quentin yes, can you suggest a solution?
|
0

As Quentin has said, the browser may already have removed this rule, so you can't really edit it.

One the other hand, you can take a look at client side css parsing. LESS has a client side version of their CSS processor, which essentially looks at the available CSS source, and modifies it before returning it to the browser. The LESS source code is open source, so you can see how it does this. Unfortunately, this is a rather complicated process and overkill for something like this. You would be much better off fixing the CSS before it gets delivered to the browser.

1 Comment

The question isn't about setting the background colour to transparent, it is about removing the invalid section from the rule. i.e. the bold bit here: #00bf00 00bf00 url
0

If you just want to change the style of an element that already exists then just do

document.body.style.background = '#00bf00 url(\'images/low.png\') repeat';

If you need to change the style itself to make it apply to future elements as well then overwrite the selector with the correct value.

$('head').append('<style>#main { background:#00bf00 url(\'images/low.png\') repeat;  }  </style>')

(this example assumes the existing style is in the head, so that the new one is inserted after the existing one)

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.