Skip to main content

How can I erase all inline styles with javascript and leave only the styles specified in the css style sheet?



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?



Source: Tips4all

Comments

  1. $('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.

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

    ReplyDelete
  3. I was using the $('div').attr('style', ''); technicque, and it wasn't working in IE8. I alerted the style attribute and it was not stripping out inline styles. .removeAttr ended up doing the trick in IE8.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.