1

I tried to change the color of the placeholder by adding a new class, so that the color property is overwritten. However, it does not seem to work. Following code is intended to change the placeholder color from green to red, however it is changed from green to default black/gray.

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <input class="class" id="id" type="" name="" placeholder="placeholder text">
</body>
<style type="text/css">
  .class::-moz-placeholder { /* Mozilla Firefox 19+ */
    color:    green;
  }
  .class2::-moz-placeholder {
    color:  red !important;
   }
</style>
<script type="text/javascript">document.getElementById("id").className+="class2";</script>
</html>

Where is the mistake or which alternative does work?

4
  • 1
    You need to separate css classes with a space, ie .className += ' class2'. Why not just replace the class though? There's nothing in class that isn't in class2 Commented Jul 7, 2016 at 1:13
  • Voting to close as a typo Commented Jul 7, 2016 at 1:14
  • indeed, i forgot, thanks for the quick response Commented Jul 7, 2016 at 1:15
  • 1
    I reduced the problem to the minimum, in the original problem there are defined more properties Commented Jul 7, 2016 at 1:18

2 Answers 2

2
document.getElementById("id").className += "class2";

turns the class name into "classclass2". Because there is no corresponding class classclass2 in your CSS, this element is ignored. Add a space before the new class name:

document.getElementById("id").className += " class2";

or use the more modern classList approach:

document.getElementById("id").classList.add("class2");
Sign up to request clarification or add additional context in comments.

Comments

1

A nice modern solution for this for ES5 ready browser is to use the classList method from a selected DOMnode. This way you will not run into concat problems when rendering fragments to the DOM.

document.getElementById("id").classList.add("class2")

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.