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;
javascript html css
add a comment |
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;
javascript html css
add a comment |
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;
javascript html css
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;
::placeholder {
color: red;
}<input placeholder="placeholder" />::placeholder {
color: red;
}<input placeholder="placeholder" />javascript html css
javascript html css
edited Feb 18 at 15:19
Temani Afif
79.9k94692
79.9k94692
asked Feb 18 at 14:26
Manuel RizzoManuel Rizzo
19513
19513
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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 variable are useful when it comes to modify pseudo-class/pseudo-element that you cannot access with JS such as :before/:after/::placeholer/::selection, etc. You simply define your property using a variable that you can easily update on the element.
Related : https://stackoverflow.com/a/49618941/8620333
1
Very nice, you just reminded me they existed. Thinking about, would be nice to have a ie11 supported version of this answer too...maybe later if I have time can contribute!
– Mel Macaluso
Feb 18 at 14:38
1
@MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)
– Temani Afif
Feb 18 at 14:41
1
@DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button
– Temani Afif
Feb 18 at 14:45
1
ok I don't understand sorry.
– dgknca
Feb 18 at 14:46
1
@TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah
– Mel Macaluso
Feb 18 at 16:00
add a comment |
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" />
How easy can this be changed to consider a dynamic selector? I suppose there is a more efficient way than this: jsfiddle.net/xqu9g7r2/1 or probably not?
– Temani Afif
Feb 20 at 21:37
@TemaniAfif You probably don't want to create a new rule on everyupdatecall, like this. But in general, yes, you need one rule per selector.
– Bergi
Feb 20 at 22:18
1
yes, the update was somehow what I am looking for. Thanks
– Temani Afif
Feb 20 at 22:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54749402%2fhow-to-update-placeholder-color-using-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 variable are useful when it comes to modify pseudo-class/pseudo-element that you cannot access with JS such as :before/:after/::placeholer/::selection, etc. You simply define your property using a variable that you can easily update on the element.
Related : https://stackoverflow.com/a/49618941/8620333
1
Very nice, you just reminded me they existed. Thinking about, would be nice to have a ie11 supported version of this answer too...maybe later if I have time can contribute!
– Mel Macaluso
Feb 18 at 14:38
1
@MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)
– Temani Afif
Feb 18 at 14:41
1
@DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button
– Temani Afif
Feb 18 at 14:45
1
ok I don't understand sorry.
– dgknca
Feb 18 at 14:46
1
@TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah
– Mel Macaluso
Feb 18 at 16:00
add a comment |
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 variable are useful when it comes to modify pseudo-class/pseudo-element that you cannot access with JS such as :before/:after/::placeholer/::selection, etc. You simply define your property using a variable that you can easily update on the element.
Related : https://stackoverflow.com/a/49618941/8620333
1
Very nice, you just reminded me they existed. Thinking about, would be nice to have a ie11 supported version of this answer too...maybe later if I have time can contribute!
– Mel Macaluso
Feb 18 at 14:38
1
@MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)
– Temani Afif
Feb 18 at 14:41
1
@DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button
– Temani Afif
Feb 18 at 14:45
1
ok I don't understand sorry.
– dgknca
Feb 18 at 14:46
1
@TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah
– Mel Macaluso
Feb 18 at 16:00
add a comment |
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 variable are useful when it comes to modify pseudo-class/pseudo-element that you cannot access with JS such as :before/:after/::placeholer/::selection, etc. You simply define your property using a variable that you can easily update on the element.
Related : https://stackoverflow.com/a/49618941/8620333
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 variable are useful when it comes to modify pseudo-class/pseudo-element that you cannot access with JS such as :before/:after/::placeholer/::selection, etc. You simply define your property using a variable that you can easily update on the element.
Related : https://stackoverflow.com/a/49618941/8620333
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>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>edited Feb 18 at 14:38
answered Feb 18 at 14:29
Temani AfifTemani Afif
79.9k94692
79.9k94692
1
Very nice, you just reminded me they existed. Thinking about, would be nice to have a ie11 supported version of this answer too...maybe later if I have time can contribute!
– Mel Macaluso
Feb 18 at 14:38
1
@MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)
– Temani Afif
Feb 18 at 14:41
1
@DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button
– Temani Afif
Feb 18 at 14:45
1
ok I don't understand sorry.
– dgknca
Feb 18 at 14:46
1
@TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah
– Mel Macaluso
Feb 18 at 16:00
add a comment |
1
Very nice, you just reminded me they existed. Thinking about, would be nice to have a ie11 supported version of this answer too...maybe later if I have time can contribute!
– Mel Macaluso
Feb 18 at 14:38
1
@MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)
– Temani Afif
Feb 18 at 14:41
1
@DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button
– Temani Afif
Feb 18 at 14:45
1
ok I don't understand sorry.
– dgknca
Feb 18 at 14:46
1
@TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah
– Mel Macaluso
Feb 18 at 16:00
1
1
Very nice, you just reminded me they existed. Thinking about, would be nice to have a ie11 supported version of this answer too...maybe later if I have time can contribute!
– Mel Macaluso
Feb 18 at 14:38
Very nice, you just reminded me they existed. Thinking about, would be nice to have a ie11 supported version of this answer too...maybe later if I have time can contribute!
– Mel Macaluso
Feb 18 at 14:38
1
1
@MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)
– Temani Afif
Feb 18 at 14:41
@MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)
– Temani Afif
Feb 18 at 14:41
1
1
@DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button
– Temani Afif
Feb 18 at 14:45
@DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button
– Temani Afif
Feb 18 at 14:45
1
1
ok I don't understand sorry.
– dgknca
Feb 18 at 14:46
ok I don't understand sorry.
– dgknca
Feb 18 at 14:46
1
1
@TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah
– Mel Macaluso
Feb 18 at 16:00
@TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah
– Mel Macaluso
Feb 18 at 16:00
add a comment |
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" />
How easy can this be changed to consider a dynamic selector? I suppose there is a more efficient way than this: jsfiddle.net/xqu9g7r2/1 or probably not?
– Temani Afif
Feb 20 at 21:37
@TemaniAfif You probably don't want to create a new rule on everyupdatecall, like this. But in general, yes, you need one rule per selector.
– Bergi
Feb 20 at 22:18
1
yes, the update was somehow what I am looking for. Thanks
– Temani Afif
Feb 20 at 22:26
add a comment |
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" />
How easy can this be changed to consider a dynamic selector? I suppose there is a more efficient way than this: jsfiddle.net/xqu9g7r2/1 or probably not?
– Temani Afif
Feb 20 at 21:37
@TemaniAfif You probably don't want to create a new rule on everyupdatecall, like this. But in general, yes, you need one rule per selector.
– Bergi
Feb 20 at 22:18
1
yes, the update was somehow what I am looking for. Thanks
– Temani Afif
Feb 20 at 22:26
add a comment |
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" />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" />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" />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" />answered Feb 18 at 22:20
BergiBergi
378k63577908
378k63577908
How easy can this be changed to consider a dynamic selector? I suppose there is a more efficient way than this: jsfiddle.net/xqu9g7r2/1 or probably not?
– Temani Afif
Feb 20 at 21:37
@TemaniAfif You probably don't want to create a new rule on everyupdatecall, like this. But in general, yes, you need one rule per selector.
– Bergi
Feb 20 at 22:18
1
yes, the update was somehow what I am looking for. Thanks
– Temani Afif
Feb 20 at 22:26
add a comment |
How easy can this be changed to consider a dynamic selector? I suppose there is a more efficient way than this: jsfiddle.net/xqu9g7r2/1 or probably not?
– Temani Afif
Feb 20 at 21:37
@TemaniAfif You probably don't want to create a new rule on everyupdatecall, like this. But in general, yes, you need one rule per selector.
– Bergi
Feb 20 at 22:18
1
yes, the update was somehow what I am looking for. Thanks
– Temani Afif
Feb 20 at 22:26
How easy can this be changed to consider a dynamic selector? I suppose there is a more efficient way than this: jsfiddle.net/xqu9g7r2/1 or probably not?
– Temani Afif
Feb 20 at 21:37
How easy can this be changed to consider a dynamic selector? I suppose there is a more efficient way than this: jsfiddle.net/xqu9g7r2/1 or probably not?
– Temani Afif
Feb 20 at 21:37
@TemaniAfif You probably don't want to create a new rule on every
update call, like this. But in general, yes, you need one rule per selector.– Bergi
Feb 20 at 22:18
@TemaniAfif You probably don't want to create a new rule on every
update call, like this. But in general, yes, you need one rule per selector.– Bergi
Feb 20 at 22:18
1
1
yes, the update was somehow what I am looking for. Thanks
– Temani Afif
Feb 20 at 22:26
yes, the update was somehow what I am looking for. Thanks
– Temani Afif
Feb 20 at 22:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54749402%2fhow-to-update-placeholder-color-using-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown