views:

743

answers:

3

If I have the following in my html:

<div style="height:300px; width:300px; background-color:#ffffff;"></div>

And this in my css style sheet:

div {
    width:100px;
    height:100px;
    background-color:#000000;
}

Is there any way, with javascript/jquery, to remove all of the inline styles and leave only the styles specified by the css style sheet?

+11  A: 

$('div').attr('style', '');

or

$('div').removeAttr('style'); (From Andres's Answer)

To make this a little smaller, try this:

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

This should speed it up a little because it checks that the divs have the style attribute.

Either way, this might take a little while to process if you have a large amount of divs, so you might want to consider other methods than javascript.

Chacha102
+6  A: 
$('div').removeAttr('style');
andres descalzo
Is this more efficient than doing attr('style', '')?
Matt
with $('div').attr('style', '');<div style=""></div>with $('div').removeAttr('style');<div></div>is more clean
andres descalzo
probably, because it isn't setting it to a null value, but either way you are going to get a large processing time because it has to go through every div in the body.
Chacha102
yes, that's clear, it should be changed to an ID $("#id") or a class $ (".classDivs)
andres descalzo
A: 

You could also try listing the css in the style sheet as !important

SpeedGun