Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change placeholder text color

Is it possible to use "::-webkit-input-placeholder" with jQuery to set a color for the placeholder text?

Something like this:

$("input::-webkit-input-placeholder").css({"color" : "#b2cde0"}); 
like image 965
Benny Avatar asked Feb 19 '13 20:02

Benny


People also ask

How can I change placeholder color?

<input type="text" placeholder="A red placeholder text..">

How do I change placeholder text?

The “placeholder” property is used to get or set the placeholder of an input text. This can be used to change the placeholder text to a new one. The element is first selected using a jQuery selector. The new placeholder text can then be assigned to the element's placeholder property.

How can I change the text color with jQuery?

To change the text color with jQuery, use the jQuery css() method. The color css property is used to change text color.

What is the default placeholder text color?

Note: In most browsers, the appearance of placeholder text is a translucent or light gray color by default.


2 Answers

You can't really modify pseudo-selectors with JavaScript. You'll have to modify an existing a <style> element.

If possible, make a class:

.your-class::-webkit-input-placeholder {     color: #b2cde0 } 

And add it to the element:

 $('input').addClass('your-class'); 
like image 182
Blender Avatar answered Sep 17 '22 20:09

Blender


If you don't have a stylesheet, and want to inject CSS dynamically you can use the following:

$('body').append('<style>.my_class::placeholder{color:red}</style>') 

Just use my_class and the color for the placeholder.

It's handy to have a general purpose function:

function change_placeholder_color(target_class, color_choice) {     $("body").append("<style>" + target_class + "::placeholder{color:" +  color_choice + "}</style>") } 

Usage:

change_placeholder_color('.my_input', 'red') 
like image 39
Cybernetic Avatar answered Sep 20 '22 20:09

Cybernetic