Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color of placeholder text on focus with JavaScript [closed]

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

like image 553
NohmanJ Avatar asked Oct 22 '15 16:10

NohmanJ


People also ask

How do I change the color of the placeholder text?

In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector.

How do I change the opacity of a placeholder in Firefox?

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.

How do I add a red placeholder in HTML?

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.

What is the use of placeholder text in HTML?

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.


1 Answers

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/

like image 127
AtheistP3ace Avatar answered Sep 21 '22 22:09

AtheistP3ace