It is possible to assign the text:
inputElement.placehoder="my placeholder";
But how to adjust placeholder's color or opacity (without creating css selectors) ?
It is possible to assign the text:
inputElement.placehoder="my placeholder";
But how to adjust placeholder's color or opacity (without creating css selectors) ?
Just add a class say, custom-placeholder
attached to a predefined ::placeholder
selector to your css and then use JavaScript to add it to your element like this:
.custom-placeholder::-webkit-input-placeholder {
color: red;
opacity: 0.4;
}
.custom-placeholder:-ms-input-placeholder {
color: red;
opacity: 0.4;
}
.custom-placeholder::placeholder {
color: red;
opacity: 0.4;
}
Check and run the following Code Snippet for a practical example of the above:
document.getElementById("input").classList.add("custom-placeholder");
.custom-placeholder::-webkit-input-placeholder {
color: red;
opacity: 0.4;
}
.custom-placeholder:-ms-input-placeholder {
color: red;
opacity: 0.4;
}
.custom-placeholder::placeholder {
color: red;
opacity: 0.4;
}
<input id="input" type="text" placeholder="hello world" />
<input id="input2" type="text" placeholder="this would not be affected" />
You will need to define classes for the different appearances you want and then apply those classes through your JS.
See the answer to this question to do that: jQuery change placeholder text color
You can't; only the pseudo-element ::placeholder
in CSS can effect the color
At best, you can apply this styling to a class / id, and then use JavaScript to add that class to your element
::placeholder {
color: blue;
}
<input placeholder="I should be blue" />