How to update placeholder color using Javascript?












9















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;









share|improve this question





























    9















    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;









    share|improve this question



























      9












      9








      9








      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;









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 13 hours ago









      Temani Afif

      73.7k94184




      73.7k94184










      asked 14 hours ago









      Manuel RizzoManuel Rizzo

      14512




      14512
























          2 Answers
          2






          active

          oldest

          votes


















          24














          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






          share|improve this answer


























          • 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
            14 hours ago











          • @MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)

            – Temani Afif
            14 hours ago













          • @DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button

            – Temani Afif
            14 hours ago











          • ok I don't understand sorry.

            – Dogukan Cavus
            14 hours ago













          • @TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah

            – Mel Macaluso
            12 hours ago



















          0














          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" />








          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            24














            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






            share|improve this answer


























            • 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
              14 hours ago











            • @MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)

              – Temani Afif
              14 hours ago













            • @DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button

              – Temani Afif
              14 hours ago











            • ok I don't understand sorry.

              – Dogukan Cavus
              14 hours ago













            • @TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah

              – Mel Macaluso
              12 hours ago
















            24














            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






            share|improve this answer


























            • 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
              14 hours ago











            • @MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)

              – Temani Afif
              14 hours ago













            • @DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button

              – Temani Afif
              14 hours ago











            • ok I don't understand sorry.

              – Dogukan Cavus
              14 hours ago













            • @TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah

              – Mel Macaluso
              12 hours ago














            24












            24








            24







            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






            share|improve this answer















            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>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 14 hours ago

























            answered 14 hours ago









            Temani AfifTemani Afif

            73.7k94184




            73.7k94184













            • 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
              14 hours ago











            • @MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)

              – Temani Afif
              14 hours ago













            • @DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button

              – Temani Afif
              14 hours ago











            • ok I don't understand sorry.

              – Dogukan Cavus
              14 hours ago













            • @TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah

              – Mel Macaluso
              12 hours ago



















            • 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
              14 hours ago











            • @MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)

              – Temani Afif
              14 hours ago













            • @DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button

              – Temani Afif
              14 hours ago











            • ok I don't understand sorry.

              – Dogukan Cavus
              14 hours ago













            • @TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah

              – Mel Macaluso
              12 hours ago

















            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
            14 hours ago





            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
            14 hours ago













            @MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)

            – Temani Afif
            14 hours ago







            @MelMacaluso for the ie11 version we can use a class we toggle that contains the color (not very flexible though)

            – Temani Afif
            14 hours ago















            @DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button

            – Temani Afif
            14 hours ago





            @DogukanCavus red is the default value ... blue will be set to only the first one when clicking the button

            – Temani Afif
            14 hours ago













            ok I don't understand sorry.

            – Dogukan Cavus
            14 hours ago







            ok I don't understand sorry.

            – Dogukan Cavus
            14 hours ago















            @TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah

            – Mel Macaluso
            12 hours ago





            @TemaniAfif of course we can, I was hinting how we can do that with flexibility ahah

            – Mel Macaluso
            12 hours ago













            0














            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" />








            share|improve this answer




























              0














              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" />








              share|improve this answer


























                0












                0








                0







                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" />








                share|improve this answer













                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" />






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 6 hours ago









                BergiBergi

                372k58557884




                372k58557884






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Усть-Каменогорск

                    Халкинская богословская школа

                    Высокополье (Харьковская область)