34

On a php generated page there are several elements like this:

<td class="defaultTDStyle" style="color:userDefinedCustomColor" id="myTDId"></td>

So there is a default style and I apply several extra styles that override the style defined in the CSS.

Is there a way to remove these added styles from javascript? It seems the obj.style.color="default" and obj.style.color="auto" don't work. How can I reset the color to the CSS default from javascript?

6 Answers 6

50

If recollection serves, obj.style.color="" should work... I don't know if it's right though.

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

9 Comments

I think that only resets styles set via JavaScript. But in this case, the original style is inline.
It works, and it is right, although since the style is added in markup, you might want to do obj.removeAttribute('style') as well for good measure.
@casablanca well it sure does work in Firefox, regardless of where the on-element style was set.
@MooGoo - I think removing the whole style attribute could be drastic. What if there are other properties they don't want to reset/remove?
Works in Opera and Chrome too.
|
15

Set the style property values to the empty string:

 obj.style.color = "";

1 Comment

Setting to null works in ff and chrome but not in IE. In IE (and this works in FF and chrome) you should do obj.style.color="";
7

The new way:

el.attributeStyleMap.delete('color')

or clear everything:

el.attributeStyleMap.clear()

Not all browsers support this yet though. See StylePropertyMap on MDN for more details and browser compatibility. Since Firefox currently does not support it, you should not use this in production.

See also CSS Typed Object Model and Working with the new CSS Typed Object Model.

1 Comment

1

May 2025 update:

There are two fully cross-browser options:

  1. Use null (recommended):
element.style.width = null;
  1. Use an empty string:
element.style.width = "";

I would recommend using the first one to avoid accidental bugs where a string is not really empty (" " for example).

P.S. Option #1 might also be implemented using more explanatory approach:

function resetWidth() {
  element.style.width = null;
}

A style declaration is reset by setting it to null or an empty string

MDN.

Comments

0

You could save the attribute style before overriding it, and then apply it again. Like this:

// Get element styling
const element = document.getElementById('someId');
const style = element.getAttribute('style');
const classList = element.classList;
/** Code for overriding element styling goes here **/ 
// Retrieve original styling
const newClassList = element.classList;
for (let item of newClassList) 
    element.classList.remove(item);
for (let item of classList)
    element.classList.add(item);
element.setAttribute('style', style);

Comments

0

If you previously set the style via JS, you can do:

element.style.color = null;

This resets the style declaration. More info here:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style

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.