Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update placeholder color using Javascript?

I'm searching online and I didn't find anything. I'm trying to update the placeholder color of a textbox using javascript, but how can I do that? I have a color picker and the color is changing.

If I have something like this in my CSS, how can I update it?

::placeholder {    color: red;  }
<input placeholder="placeholder" />

Is there a javascript command to edit this? Something like

document.getElementById('text').style.placeholderColor = newColor; 
like image 328
Manuel Rizzo Avatar asked Feb 18 '19 14:02

Manuel Rizzo


People also ask

How can I change placeholder color?

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

How do you target a placeholder in JavaScript?

To change the placeholder text of an input element with JavaScript, we can set the placeholder property of an element. Then we can set the placeholder attribute value for each input element by writing: document. getElementsByName('Email')[0].

How do I change the placeholder color on a storyboard?

The easiest method to modify the placeholder text color is through the Xcode storyboard interface builder. Select the UITextField of interest and open the identity inspector on the right. Click on the plus symbol in the User Defined Runtime Attributes and add a new row with Key Path as placeholderLabel.

What is placeholder color code?

So - Bootstrap 3 defines the default placeholder color as #999 .


2 Answers

Use CSS variables. You can also target only the needed element

function update() {    document.querySelector('input[type=text]').style.setProperty("--c", "blue");  }
::placeholder {    color: var(--c, red);  }
<input type="text" placeholder="I will be blue">  <input type="number" placeholder="I will remain red">  <button onclick="update()">change</button>

CSS variables are useful when it comes to modify pseudo elements that you cannot access with JS such as :before/:after/::placeholer/::selection, etc. You simply define your custom property that you can easily update on the main element and the pseudo element will inherit it.

Related : Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery

like image 180
Temani Afif Avatar answered Sep 19 '22 15:09

Temani Afif


As stated in the other answers, you cannot change pseudo-element styles inline. However, you can modify the CSS rule in the <style> itself, and you don't need a browser support ing CSS variables for that. Access the stylesheet and either get the existing rule or insert your own, then play with its style declarations like you would with an element .style:

const {sheet} = Object.assign(document.head.appendChild(document.createElement("style")), {type: "text/css" });  const placeholderStyle = sheet.rules[sheet.insertRule("::placeholder {}")].style;  placeholderStyle.color = "red";    Object.assign(document.body.appendChild(document.createElement("input")), {    type: "button", value: "Color!", onclick() {      placeholderStyle.color = "#"+Math.round(Math.random()*0xFFF).toString(16).padStart("0",3);  }});
<input placeholder="placeholder" />
like image 37
Bergi Avatar answered Sep 18 '22 15:09

Bergi