Detect if input placeholder is visible












3















I'm trying to find a way to detect if an input is currently showing a placeholder.



I know we can test whether or not placeholders are supported in a given browser, which I would use, but that's not what I'm asking here.



The :placeholder-shown pseudo class does exactly what I need, but the support for it is very low. Much lower than the support for placeholders in general. So I'm looking for an alternative method.



Note: The solution cannot rely on whether or not the input has changed or gained a value. Autofilled inputs neither have a technical value, nor have changed. Therefore the solution needs to truly detect the placeholder it'self.










share|improve this question



























    3















    I'm trying to find a way to detect if an input is currently showing a placeholder.



    I know we can test whether or not placeholders are supported in a given browser, which I would use, but that's not what I'm asking here.



    The :placeholder-shown pseudo class does exactly what I need, but the support for it is very low. Much lower than the support for placeholders in general. So I'm looking for an alternative method.



    Note: The solution cannot rely on whether or not the input has changed or gained a value. Autofilled inputs neither have a technical value, nor have changed. Therefore the solution needs to truly detect the placeholder it'self.










    share|improve this question

























      3












      3








      3








      I'm trying to find a way to detect if an input is currently showing a placeholder.



      I know we can test whether or not placeholders are supported in a given browser, which I would use, but that's not what I'm asking here.



      The :placeholder-shown pseudo class does exactly what I need, but the support for it is very low. Much lower than the support for placeholders in general. So I'm looking for an alternative method.



      Note: The solution cannot rely on whether or not the input has changed or gained a value. Autofilled inputs neither have a technical value, nor have changed. Therefore the solution needs to truly detect the placeholder it'self.










      share|improve this question














      I'm trying to find a way to detect if an input is currently showing a placeholder.



      I know we can test whether or not placeholders are supported in a given browser, which I would use, but that's not what I'm asking here.



      The :placeholder-shown pseudo class does exactly what I need, but the support for it is very low. Much lower than the support for placeholders in general. So I'm looking for an alternative method.



      Note: The solution cannot rely on whether or not the input has changed or gained a value. Autofilled inputs neither have a technical value, nor have changed. Therefore the solution needs to truly detect the placeholder it'self.







      javascript jquery placeholder






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 13 '16 at 21:41









      ChaseChase

      99311018




      99311018
























          3 Answers
          3






          active

          oldest

          votes


















          2














          First, check to see if the placeholder attribute is being used by the element, and then check to see if the value of the input is empty:






          function placeholderActive(selector) {
          var el = document.querySelector(selector);
          if (el.getAttribute('placeholder') && el.value === '') {
          return true;
          }
          return false;
          }


          var a = placeholderActive('#test1'); // false
          var b = placeholderActive('#test2'); // false
          var c = placeholderActive('#test3'); // false
          var d = placeholderActive('#test4'); // true

          console.log(a, b, c, d);

          <input id="test1" name="test1" value="123">
          <input id="test2" name="test2" placeholder="" value="123">
          <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
          <input id="test4" name="test4" placeholder="Another placeholder" value="">








          share|improve this answer

































            1














            Add the required attribute to the input and then use :invalid to select the input that shows the placeholder.



            This does not work with input types like email or inputs with a pattern attribute.



            Using js is still the best option if you want it to work properly in all cases.






            share|improve this answer
























            • Thanks for the reply, but the required method only covers specific use cases, which many forms don't fall within. I'm looking for something less constrained.

              – Chase
              Feb 13 '16 at 21:57



















            1














            Nowadays, for most browsers we can use :placeholder-shown pseudo-class to detect whether placeholder is shown or not.






            function placeholderActive(selector) {
            return !!document.querySelector(selector + ':placeholder-shown');
            }
            const a = placeholderActive('#test1'); // false
            const b = placeholderActive('#test2'); // false
            const c = placeholderActive('#test3'); // false
            const d = placeholderActive('#test4'); // true

            console.log(a, b, c, d);

            <input id="test1" name="test1" value="123">
            <input id="test2" name="test2" placeholder="" value="123">
            <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
            <input id="test4" name="test4" placeholder="Another placeholder" value="">





            CSS-Tricks: https://css-tricks.com/almanac/selectors/p/placeholder-shown/



            CanIUse: https://caniuse.com/#feat=css-placeholder-shown






            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%2f35385653%2fdetect-if-input-placeholder-is-visible%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              First, check to see if the placeholder attribute is being used by the element, and then check to see if the value of the input is empty:






              function placeholderActive(selector) {
              var el = document.querySelector(selector);
              if (el.getAttribute('placeholder') && el.value === '') {
              return true;
              }
              return false;
              }


              var a = placeholderActive('#test1'); // false
              var b = placeholderActive('#test2'); // false
              var c = placeholderActive('#test3'); // false
              var d = placeholderActive('#test4'); // true

              console.log(a, b, c, d);

              <input id="test1" name="test1" value="123">
              <input id="test2" name="test2" placeholder="" value="123">
              <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
              <input id="test4" name="test4" placeholder="Another placeholder" value="">








              share|improve this answer






























                2














                First, check to see if the placeholder attribute is being used by the element, and then check to see if the value of the input is empty:






                function placeholderActive(selector) {
                var el = document.querySelector(selector);
                if (el.getAttribute('placeholder') && el.value === '') {
                return true;
                }
                return false;
                }


                var a = placeholderActive('#test1'); // false
                var b = placeholderActive('#test2'); // false
                var c = placeholderActive('#test3'); // false
                var d = placeholderActive('#test4'); // true

                console.log(a, b, c, d);

                <input id="test1" name="test1" value="123">
                <input id="test2" name="test2" placeholder="" value="123">
                <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                <input id="test4" name="test4" placeholder="Another placeholder" value="">








                share|improve this answer




























                  2












                  2








                  2







                  First, check to see if the placeholder attribute is being used by the element, and then check to see if the value of the input is empty:






                  function placeholderActive(selector) {
                  var el = document.querySelector(selector);
                  if (el.getAttribute('placeholder') && el.value === '') {
                  return true;
                  }
                  return false;
                  }


                  var a = placeholderActive('#test1'); // false
                  var b = placeholderActive('#test2'); // false
                  var c = placeholderActive('#test3'); // false
                  var d = placeholderActive('#test4'); // true

                  console.log(a, b, c, d);

                  <input id="test1" name="test1" value="123">
                  <input id="test2" name="test2" placeholder="" value="123">
                  <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                  <input id="test4" name="test4" placeholder="Another placeholder" value="">








                  share|improve this answer















                  First, check to see if the placeholder attribute is being used by the element, and then check to see if the value of the input is empty:






                  function placeholderActive(selector) {
                  var el = document.querySelector(selector);
                  if (el.getAttribute('placeholder') && el.value === '') {
                  return true;
                  }
                  return false;
                  }


                  var a = placeholderActive('#test1'); // false
                  var b = placeholderActive('#test2'); // false
                  var c = placeholderActive('#test3'); // false
                  var d = placeholderActive('#test4'); // true

                  console.log(a, b, c, d);

                  <input id="test1" name="test1" value="123">
                  <input id="test2" name="test2" placeholder="" value="123">
                  <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                  <input id="test4" name="test4" placeholder="Another placeholder" value="">








                  function placeholderActive(selector) {
                  var el = document.querySelector(selector);
                  if (el.getAttribute('placeholder') && el.value === '') {
                  return true;
                  }
                  return false;
                  }


                  var a = placeholderActive('#test1'); // false
                  var b = placeholderActive('#test2'); // false
                  var c = placeholderActive('#test3'); // false
                  var d = placeholderActive('#test4'); // true

                  console.log(a, b, c, d);

                  <input id="test1" name="test1" value="123">
                  <input id="test2" name="test2" placeholder="" value="123">
                  <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                  <input id="test4" name="test4" placeholder="Another placeholder" value="">





                  function placeholderActive(selector) {
                  var el = document.querySelector(selector);
                  if (el.getAttribute('placeholder') && el.value === '') {
                  return true;
                  }
                  return false;
                  }


                  var a = placeholderActive('#test1'); // false
                  var b = placeholderActive('#test2'); // false
                  var c = placeholderActive('#test3'); // false
                  var d = placeholderActive('#test4'); // true

                  console.log(a, b, c, d);

                  <input id="test1" name="test1" value="123">
                  <input id="test2" name="test2" placeholder="" value="123">
                  <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                  <input id="test4" name="test4" placeholder="Another placeholder" value="">






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 1 '16 at 17:05

























                  answered Feb 13 '16 at 21:57









                  BowserBowser

                  415




                  415

























                      1














                      Add the required attribute to the input and then use :invalid to select the input that shows the placeholder.



                      This does not work with input types like email or inputs with a pattern attribute.



                      Using js is still the best option if you want it to work properly in all cases.






                      share|improve this answer
























                      • Thanks for the reply, but the required method only covers specific use cases, which many forms don't fall within. I'm looking for something less constrained.

                        – Chase
                        Feb 13 '16 at 21:57
















                      1














                      Add the required attribute to the input and then use :invalid to select the input that shows the placeholder.



                      This does not work with input types like email or inputs with a pattern attribute.



                      Using js is still the best option if you want it to work properly in all cases.






                      share|improve this answer
























                      • Thanks for the reply, but the required method only covers specific use cases, which many forms don't fall within. I'm looking for something less constrained.

                        – Chase
                        Feb 13 '16 at 21:57














                      1












                      1








                      1







                      Add the required attribute to the input and then use :invalid to select the input that shows the placeholder.



                      This does not work with input types like email or inputs with a pattern attribute.



                      Using js is still the best option if you want it to work properly in all cases.






                      share|improve this answer













                      Add the required attribute to the input and then use :invalid to select the input that shows the placeholder.



                      This does not work with input types like email or inputs with a pattern attribute.



                      Using js is still the best option if you want it to work properly in all cases.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 13 '16 at 21:52









                      seahorsepipseahorsepip

                      2,81411019




                      2,81411019













                      • Thanks for the reply, but the required method only covers specific use cases, which many forms don't fall within. I'm looking for something less constrained.

                        – Chase
                        Feb 13 '16 at 21:57



















                      • Thanks for the reply, but the required method only covers specific use cases, which many forms don't fall within. I'm looking for something less constrained.

                        – Chase
                        Feb 13 '16 at 21:57

















                      Thanks for the reply, but the required method only covers specific use cases, which many forms don't fall within. I'm looking for something less constrained.

                      – Chase
                      Feb 13 '16 at 21:57





                      Thanks for the reply, but the required method only covers specific use cases, which many forms don't fall within. I'm looking for something less constrained.

                      – Chase
                      Feb 13 '16 at 21:57











                      1














                      Nowadays, for most browsers we can use :placeholder-shown pseudo-class to detect whether placeholder is shown or not.






                      function placeholderActive(selector) {
                      return !!document.querySelector(selector + ':placeholder-shown');
                      }
                      const a = placeholderActive('#test1'); // false
                      const b = placeholderActive('#test2'); // false
                      const c = placeholderActive('#test3'); // false
                      const d = placeholderActive('#test4'); // true

                      console.log(a, b, c, d);

                      <input id="test1" name="test1" value="123">
                      <input id="test2" name="test2" placeholder="" value="123">
                      <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                      <input id="test4" name="test4" placeholder="Another placeholder" value="">





                      CSS-Tricks: https://css-tricks.com/almanac/selectors/p/placeholder-shown/



                      CanIUse: https://caniuse.com/#feat=css-placeholder-shown






                      share|improve this answer




























                        1














                        Nowadays, for most browsers we can use :placeholder-shown pseudo-class to detect whether placeholder is shown or not.






                        function placeholderActive(selector) {
                        return !!document.querySelector(selector + ':placeholder-shown');
                        }
                        const a = placeholderActive('#test1'); // false
                        const b = placeholderActive('#test2'); // false
                        const c = placeholderActive('#test3'); // false
                        const d = placeholderActive('#test4'); // true

                        console.log(a, b, c, d);

                        <input id="test1" name="test1" value="123">
                        <input id="test2" name="test2" placeholder="" value="123">
                        <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                        <input id="test4" name="test4" placeholder="Another placeholder" value="">





                        CSS-Tricks: https://css-tricks.com/almanac/selectors/p/placeholder-shown/



                        CanIUse: https://caniuse.com/#feat=css-placeholder-shown






                        share|improve this answer


























                          1












                          1








                          1







                          Nowadays, for most browsers we can use :placeholder-shown pseudo-class to detect whether placeholder is shown or not.






                          function placeholderActive(selector) {
                          return !!document.querySelector(selector + ':placeholder-shown');
                          }
                          const a = placeholderActive('#test1'); // false
                          const b = placeholderActive('#test2'); // false
                          const c = placeholderActive('#test3'); // false
                          const d = placeholderActive('#test4'); // true

                          console.log(a, b, c, d);

                          <input id="test1" name="test1" value="123">
                          <input id="test2" name="test2" placeholder="" value="123">
                          <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                          <input id="test4" name="test4" placeholder="Another placeholder" value="">





                          CSS-Tricks: https://css-tricks.com/almanac/selectors/p/placeholder-shown/



                          CanIUse: https://caniuse.com/#feat=css-placeholder-shown






                          share|improve this answer













                          Nowadays, for most browsers we can use :placeholder-shown pseudo-class to detect whether placeholder is shown or not.






                          function placeholderActive(selector) {
                          return !!document.querySelector(selector + ':placeholder-shown');
                          }
                          const a = placeholderActive('#test1'); // false
                          const b = placeholderActive('#test2'); // false
                          const c = placeholderActive('#test3'); // false
                          const d = placeholderActive('#test4'); // true

                          console.log(a, b, c, d);

                          <input id="test1" name="test1" value="123">
                          <input id="test2" name="test2" placeholder="" value="123">
                          <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                          <input id="test4" name="test4" placeholder="Another placeholder" value="">





                          CSS-Tricks: https://css-tricks.com/almanac/selectors/p/placeholder-shown/



                          CanIUse: https://caniuse.com/#feat=css-placeholder-shown






                          function placeholderActive(selector) {
                          return !!document.querySelector(selector + ':placeholder-shown');
                          }
                          const a = placeholderActive('#test1'); // false
                          const b = placeholderActive('#test2'); // false
                          const c = placeholderActive('#test3'); // false
                          const d = placeholderActive('#test4'); // true

                          console.log(a, b, c, d);

                          <input id="test1" name="test1" value="123">
                          <input id="test2" name="test2" placeholder="" value="123">
                          <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                          <input id="test4" name="test4" placeholder="Another placeholder" value="">





                          function placeholderActive(selector) {
                          return !!document.querySelector(selector + ':placeholder-shown');
                          }
                          const a = placeholderActive('#test1'); // false
                          const b = placeholderActive('#test2'); // false
                          const c = placeholderActive('#test3'); // false
                          const d = placeholderActive('#test4'); // true

                          console.log(a, b, c, d);

                          <input id="test1" name="test1" value="123">
                          <input id="test2" name="test2" placeholder="" value="123">
                          <input id="test3" name="test3" placeholder="Some Placeholder" value="123">
                          <input id="test4" name="test4" placeholder="Another placeholder" value="">






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 21 '18 at 4:21









                          Enzam HossainEnzam Hossain

                          33926




                          33926






























                              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%2f35385653%2fdetect-if-input-placeholder-is-visible%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

                              android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                              SQL update select statement

                              WPF add header to Image with URL pettitions [duplicate]