Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the colour of placeholder using javascript? [duplicate]

I want to change the colour of this placeholder after calling mobileValidate().

<input type="text" class="form-control" name="InputMobile" id="Mobile"    placeholder="Enter Mobile Number" onblur="mobileValidate()"required>

JavaScript function is

function mobileValidate(){

    var x = document.getElementById("Mobile");

        if ((x.value).match(re)){
            alert("mobile Number is valid");
        }
        else{

            alert("mobile no is not valid");
            x.value="";
            x.placeholder.style.color="red";
        }

}
like image 994
Rahul Singh Avatar asked May 08 '15 07:05

Rahul Singh


People also ask

How do I change the color of a placeholder?

In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector. Note that Firefox adds a lower opacity to the placeholder, so we use opacity: 1 to fix this.

What is placeholder color code?

The default input placeholder color varies by browser. In your screenshot, it's #8e8e8e. Some examples by browser: In Chrome (Mac) it's #a9a9a9. in Firefox (Mac) it's #777777.

How do I change placeholders?

Select the placeholder, position the pointer over a sizing handle, and then drag the handle until the placeholder is the size that you want. Select the placeholder, and then drag it to its new location. Select the placeholder, click the Format tab, and then make the changes that you want.

How do you style placeholder text?

Use the ::placeholder pseudo-element to style your placeholder text in an <input> or <textarea> form element. Most modern browsers support this, but for older browsers, vendor prefixes will be required.


1 Answers

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

If possible, make a class:

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

And add it to the element:

$('input').addClass('your-class');

Or if you want to use pure JS, do this:

x.classList.add('your-class');
like image 146
danish farhaj Avatar answered Oct 21 '22 00:10

danish farhaj