The task is simple. The site has a feedback form. By default, text fields contain some values. How to make it so that when you click on the field, the original text disappears and the user can enter his info? I try this way, it does not work out for some reason:

<td onclick='document.getElementById('name').value="";'>{name:body}</td> 

{name:body} is the insertion of the input itself into the form. ID=name at INPUT .

    3 answers 3

    There is a simple option, this is added to the text field:

     onblur="javascript:if(this.value==''){this.value='Enter Search keywords'};" onfocus="if(this.value=='Enter Search keywords') {this.value='';}" value="Enter Search keywords" 

    But I recommend more beautiful here http://gword.ru/ (fields in the form Leave an application). Firebug makes it easy to understand how it works;

    • the problem is that I can register an event only for the table cell in which the text field itself is located ... can you explain to me why my version does not work? - Nuboyd
    • one
      Quotes, I think, this should work: <td onclick = "document.getElementById ('name'). Value = \ '\';"> {name: body} </ td> - Dem

    Initially, everything was right with you, the only thing that needed to be changed is the quotes '' in getElementById to "" ", the script saw here a single quote as closing the onclick. It was necessary so:

     onclick='document.getElementById("name").value="";' 

      By such methods my great-great-grandfather made it out. Now they make it easier:

       <input type="text" placeholder="Поиск" value="" /> 

      And any polyfill for old browsers from the “ Web Forms: input placeholder ” section, for example, Enhanced jQuery Placeholder plugin .

      Update: If you can’t get to <input> (the template engine interferes, etc.), then you can always say something in the spirit

       <script> $("input[name='search']").prop("placeholder", "Поиск"); // Или .attr, не соображу с ходу, а проверять лень. </script> 

      At the same time will not interfere with browsers without JavaScript.

      • What does this have to do with layout? O_o - Zowie
      • Is HTML something like programming? - drdaeman pm
      • What does javascript have to do with html? - Zowie
      • one
        Well, the question is not about JS, but about the input field ( <input> , i.e., about HTML). And what it is done is the tenth case. And the onblur / onfocus attributes are onblur layout (and, according to trends, they do not write them now, they prefer to hang handlers in a different way, without interfering with the logic view). Programming is inside. - drdaeman