I'm trying to change the color of placeholder text to white on focus. You can view the contact form here.
I've tried different CSS code, but most of the code doesn't look same on different browsers + I've done some research, and I can now see that there are some limits when it comes to styling the placeholders with CSS.
My question is, can I change the placeholder color to white on focus with JavaScript? I'm not so familiar with writing JavaScript, but would appreciate any help
In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector.
If you want to change it, you need to use the ::placeholder pseudo-element. Note that Firefox adds lower opacity to the placeholder, so use opacity: 1; to fix it. In the case you want to select the input itself when it's placeholder text is being shown, use the :placeholder-shown pseudo-class.
Try it Yourself » Step 1) Add HTML: Use an input element and add the placeholder attribute: Example <input type="text" placeholder="A red placeholder text.."> Step 2) Add CSS: In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholderselector.
This attribute being used on the <input> and <textarea> elements provides a hint to the user of what can be entered in the field. The default color of a placeholder text is light grey in most browsers.
Believe you need vendor prefixes (From css-tricks.com):
::-webkit-input-placeholder {
color: red;
}
:-moz-placeholder { /* Firefox 18- */
color: red;
}
::-moz-placeholder { /* Firefox 19+ */
color: red;
}
:-ms-input-placeholder {
color: red;
}
Using javascript you would only be applying similar styling (using vendor prefixes) programmatically on a focus event.
Edit: In fact these styles I don't think can be applied using javascript. You would need to create a class and apply that using js.
CSS:
input.placeholderred::-webkit-input-placeholder {
color: red;
}
JQuery:
var $textInput = $('#TextField1');
$textInput.on('focusin',
function () {
$(this).addClass('placeholderred');
}
);
$textInput.on('focusout',
function () {
$(this).removeClass('placeholderred');
}
);
JS:
var textInput = document.getElementById('TextField1');
textInput.addEventListener('focus',
function () {
this.classList.add('placeholderred');
}
);
textInput.addEventListener('blur',
function () {
this.classList.remove('placeholderred');
}
);
And courtesy of the most helpful, Armfoot, a fiddle: http://jsfiddle.net/qbkkabra/2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With