Remove a specific inline style with Javascript|jQuery

For those that aren't using jQuery, you can delete specific styles from the inline styles using the native removeProperty method. Example:

elem.style.removeProperty('font-family');

Of course, IE < 9 doesn't support this so you'll have to use

elem.style.removeAttribute('font-family');

so a cross browser way to do it would be:

if (elem.style.removeProperty) {
    elem.style.removeProperty('font-family');
} else {
    elem.style.removeAttribute('font-family');
}

Set the properties to inherit:

$('#foo').css('font-family','inherit').css('font-size','inherit');

I think there is no proper solution to this problem (without changing your markup). You could search and replace the style attribute's value:

var element = $('#foo');
element.attr('style', element.attr('style').replace(/font-size:[^;]+/g, '').replace(/font-family:[^;]+/g, ''))

By far the best solution would be to get rid of the inline styles and manage the styles by using classes.


In 2019, the simplest way to remove a property seems to be:

elem.style.border = "";

Similarly, to set a border:

elem.style.outline = "1px solid blue";

Should work across all browsers too!


My suggestion would be to stay away from setting this stuff using inline styles. I would suggest using classes and then using jQuery to switch between them:

CSS:

#foo{ font-size:11pt; font-family:arial; color:#000; }
#foo.highlight {text-align:center; font-size:14pt; font-family:verdana; color:red}

HTML:

<p id="foo" class="highlight">hello world</p>

Javascript:

$('#foo').removeClass('highlight');
$('#foo').addClass('highlight');