How can I erase all inline styles using javascript, leaving only the styles specified in the CSS stylesheet?


Matt:

If I have the following in my html:

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

This is in my CSS stylesheet:

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

Is it possible to use javascript/jquery to remove all inline styles and keep only the styles specified by the css stylesheet?

Tyler Carter:

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

or

$('div').removeAttr('style');(taken from Andres' answer )

To make it a little smaller, try the following:

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

This should speed things up because it checks if the div has style attributes.

Either way, this can take some time to process if you have a lot of divs, so you might want to consider something other than JavaScript.

Related


How to disable all inline styles using JavaScript?

Sobin Augustine All inline styles need to be disabled for every DOM element in the html document. Saw some solutions to replace inline styles, but I need to clear all inline styles. It could also be done using code formatting, but that's not of interest either

How to get inline styles of custom CSS vars using JavaScript

Johnny How to get inline style of custom CSS variable from element? I can easily get the width or any other traditional style, but I get an error when trying to get the custom inline style. Is there a way to extract this style data? Thanks in advance for any h

How to get inline styles of custom CSS vars using JavaScript

Johnny How to get inline style of custom CSS variable from element? I can easily get the width or any other traditional style, but I get an error when trying to get the custom inline style. Is there a way to extract this style data? Thanks in advance for any h

How to get inline styles of custom CSS vars using JavaScript

Johnny How to get inline style of custom CSS variable from element? I can easily get the width or any other traditional style, but I get an error when trying to get the custom inline style. Is there a way to extract this style data? Thanks in advance for any h

How to get inline styles of custom CSS vars using JavaScript

Johnny How to get inline style of custom CSS variable from element? I can easily get the width or any other traditional style, but I get an error when trying to get the custom inline style. Is there a way to extract this style data? Thanks in advance for any h

How to get inline styles of custom CSS vars using JavaScript

Johnny How to get inline style of custom CSS variable from element? I can easily get the width or any other traditional style, but I get an error when trying to get the custom inline style. Is there a way to extract this style data? Thanks in advance for any h

Convert CSS styles to inline styles via JavaScript

Vacisio I want to add all CSS styles of a specific element to its inline style property. E.g: I have got: <div id="d"></div> and: #d { background: #444444; width: 50px; height: 20px; display: inline-block; } Now I want a JavaScript function to convert the d

Convert CSS styles to inline styles via JavaScript

Vacisio I want to add all CSS styles of a specific element to its inline style property. E.g: I have got: <div id="d"></div> and: #d { background: #444444; width: 50px; height: 20px; display: inline-block; } Now I want a JavaScript function to convert the d

Convert CSS styles to inline styles via JavaScript

Vacisio I want to add all CSS styles of a specific element to its inline style property. E.g: I have got: <div id="d"></div> and: #d { background: #444444; width: 50px; height: 20px; display: inline-block; } Now I want a JavaScript function to convert the d

Convert CSS styles to inline styles via JavaScript

Vacisio I want to add all CSS styles of a specific element to its inline style property. E.g: I have got: <div id="d"></div> and: #d { background: #444444; width: 50px; height: 20px; display: inline-block; } Now I want a JavaScript function to convert the d

Get CSS styles set in a stylesheet using native Javascript

David I'm having some problems getting the styles of elements using native JS (not JQ). However, using something like elem.style.property returns "" because the actual HTML element doesn't set it. To make things more specific, one thing I need to get is the di

Get CSS styles set in a stylesheet using native Javascript

David I'm having some problems getting the styles of elements using native JS (not JQ). However, using something like elem.style.property returns "" because the actual HTML element doesn't set it. To make things more specific, one thing I need to get is the di

Add inline styles using Javascript

Curtis Crewe: I am trying to add this code to a dynamically created div element style = "width:330px;float:left;" The code to create the dynamic divis var nFilter = document.createElement('div'); nFilter.className = 'well'; nFilter.innerHTML = '<label>' + sS