Jump to content

how to achieve placeholder in html4?


yaragallamurali

Recommended Posts

I have a text field. i need to display some text by default. when user tries to types some text in the text field the default text should disappear and should allow the user to type in the text. This is same as "placeholder" in html 5. How can i achieve this in html4?

Link to comment
Share on other sites

<head>   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script></head><body><form action="" method="post">       <input type="text" id="input" value="" /></form><script type="text/javascript">      $(document).ready(function(){          var Element = "#input";          var InputText = "Full Name...";                    $(Element).val(InputText);                $(Element).bind('focus',function(){              $(this).addClass('focus');              if($(this).val()==InputText){                  $(this).val('');              }          }).bind('blur',function(){              if($(this).val()==""){                  $(this).val(InputText);                  $(this).removeClass('focus');              }          });      });    </script></body>

 

 

 

 

Edited by Tezzo
Link to comment
Share on other sites

<script>	window.onload = function () {    /* Grab all elements with a placeholder attribute */    var element = document.querySelectorAll('[placeholder]');    /* Loop through each found elements */    for (var i in element) {        /* If the element is a DOMElement and has the nodeName "INPUT" */        if (element[i].nodeType == 1 && element[i].nodeName == "INPUT") {            /* We change the value of the element to its placeholder attr value */            element[i].value = element[i].getAttribute('placeholder');            /* We change its color to a light gray */            element[i].style.color = "#777";            /* When the input is selected/clicked on */            element[i].onfocus = function (event) {                /* If the value within it is the placeholder, we clear it */                if (this.value == this.getAttribute('placeholder')) {                    this.value = "";                    /* Setting default text color */                    this.style.color = "#000";                };            };            /* We the input is left */            element[i].onblur = function (event) {                /* If the field is empty, we set its value to the placeholder */                if (this.value == "") {                    this.value = this.getAttribute('placeholder');                    this.style.color = "#777";                }            };        }    }}</script>

Try this

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...