27

I need to be able to reset an input field back to its original color after it has been possibly changed via javascript to a different value. The problem is I do not want to hard code the value in case the stylesheet changes. I would like to use the default color used on the page.

Is resetting the color like this fine, or is there a better way to do this?

$('#theinput').css('color', '');
1
  • If anyone manage to do that in a userscript for google doc... Commented Jul 15, 2021 at 9:09

3 Answers 3

28

There's no better way. It's the standard way.

Basically, there's no way to delete a CSS attribute from the style property of DOM elements. So all you can do is set the attribute value to empty and let CSS do its job of reverting back to the stylesheet.

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

Comments

18

If you wanted to do it in plain JavaScript, you could do:

document.getElementById('theinput').style.color = '';

The only issue with this, or your jQuery method, is if the HTML has a style attribute that sets the color CSS property. If you wanted to preserve that, you’d have to store it on page load.

But if it’s alright to assume there isn’t a style attribute, then you’re golden.

1 Comment

You'll want an empty string instead of null there.
0

Fine, but you have to specify the color inside quote and id of the input on what color should change.

This will work :)

$("#InputIdName").css('color', '#979797'); 

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.