40

I want to provide the user with a hint on what he needs to enter into my text field. However, when I set the value, it does not disappear once a user clicks on the text field. How can you make it disappear?

<form action="input_password.htm">
  <p>Username:<br><input name="Username" value="Enter username.." type="text" size="20" maxlength="20"></p>
</form>
Frank Vilea
  • 8,323
  • 20
  • 65
  • 86

10 Answers10

110

With a bit of JavaScript:

<input 
  value="Enter username..." 
  onfocus="if (this.value === 'Enter username...') this.value=''" ... />

HTML5 has a nice attribute for this, called placeholder:

<input placeholder="Enter username.." ... />

but this attribute is not supported in old browsers.

Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
aorcsik
  • 15,271
  • 5
  • 39
  • 49
  • 3
    To keep the hint text after a user has focused use: onfocus="if (this.value == 'Hint text..') this.value=''" value="Hint text.." onblur="if (this.value == '') this.value='Hint text..'" – Ole Henrik Skogstrøm Mar 26 '12 at 19:46
  • What if the user types `Enter username...` in the box? – TheRealChx101 Jul 06 '18 at 14:45
  • Then the text field is cleared, so using this polyfill requires some thinking ahead if the placeholder text was a possible input for the field. In this case `Enter username..` is a highly unlikely choice for a username. :) – aorcsik Jul 09 '18 at 08:52
27

the best way to give a hint is placeholder like this:

<input.... placeholder="hint".../>

Kunal
  • 271
  • 3
  • 2
  • 1
    This is the real answer. It's simple and requires no javascript or hovering to work. The question is tagged HTML with no mention of javascript. – Patrick Jan 19 '17 at 11:51
24

You'd need attach an onFocus event to the input field via Javascript:

<input type="text" onfocus="this.value=''" value="..." ... />
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • +1 for simplicity. What would be the opposite of onfocus? I tried your solution and it works great. Only problem: If the user moves his mouse out of the field the value is gone.. Can you bring it back somehow? – Frank Vilea Jun 08 '11 at 14:23
  • 2
    Ok, found it myself now. I added: onblur="if (this.value=='') this.value='Please enter whatever...'" – Frank Vilea Jun 08 '11 at 14:35
  • 4
    To keep the hint text after a user has focused use: onfocus="if (this.value == 'Hint text..') this.value=''" value="Hint text.." onblur="if (this.value == '') this.value='Hint text..'" – Ole Henrik Skogstrøm Mar 26 '12 at 19:45
  • 13
    With the uprising of HTML5 please consider using `placeholder` tag as described in aorcsik's answer. – nuala Oct 15 '12 at 17:14
  • 5
    `placeholder` is the modern answer, and this answer should really be removed. This solution is destructive. It will wipe out any value that the user has entered after they refocus back to the field. – p.campbell Jul 11 '17 at 18:02
  • What happens if the user does not fill in all the fields (i.e.: does not navigate through all of them), but just hits the submit button? Then all those "placeholders" (i.e.: values) would be submitted as input values... – U. Windl Jan 05 '23 at 09:21
13

With HTML5, you can now use the placeholder attribute like this:

<form action="demo_form.asp">
  <input type="text" name="fname" placeholder="First name"><br>
  <input type="text" name="lname" placeholder="Last name"><br>
  <input type="submit" value="Submit">
</form> 

http://www.w3schools.com/tags/att_input_placeholder.asp

J.Hendrix
  • 2,199
  • 4
  • 20
  • 26
13

I think for your situation, the easy and simple for your html input , you can probably add the attribute title

<input name="Username" value="Enter username.." type="text" size="20" maxlength="20" title="enter username">
Jasonw
  • 5,054
  • 7
  • 43
  • 48
2

I have the same problem, and I have add this code to my application and its work fine for me.

step -1 : added the jquery.placeholder.js plugin

step -2 :write the below code in your area.

$(function () {
       $('input, textarea').placeholder();
});

And now I can see placeholders on the input boxes!

2

This is exactly what you want

$(document).tooltip({ selector: "[title]",
                              placement: "top",
                              trigger: "focus",
                              animation: false}); 
<form id="form">
    <label for="myinput1">Browser tooltip appears on hover but disappears on clicking the input field. But this one persists while user is typing within the field</label>
    <input id="myinput1" type="text" title="This tooltip persists" />
    <input id="myinput2" type="text" title="This one also" />
</form>

[ref]

raghavsood33
  • 749
  • 7
  • 17
0

If you mean like a text in the background, I'd say you use a label with the input field and position it on the input using CSS, of course. With JS, you fade out the label when the input receives values and fade it in when the input is empty. In this way, it is not possible for the user to submit the description, whether by accident or intent.

afaolek
  • 8,452
  • 13
  • 45
  • 60
0

If you don't insist on the hint being displayed inside the input field, a modern solution would use a label element with the for attribute referring to the id of the input field, like this:

<form action="input_password.htm">
  <label for="username" title="This is your user name...">Username: </label><input id="username" name="Username" type="text" size="20" maxlength="20"></p>
</form>

If you click the label, the input field will get the input focus. If you hover over the label, it will show a longer explanation. Generally the label should describe well enough what the user has to enter (in the case of user name it should be very much obvious).

U. Windl
  • 3,480
  • 26
  • 54
-4

Define tooltip text

<input type="text" id="firstname" name="firstname" tooltipText="Type in your firstname in this box"> 

Initialize and configure the script

<script type="text/javascript">
var tooltipObj = new DHTMLgoodies_formTooltip();
tooltipObj.setTooltipPosition('right');
tooltipObj.setPageBgColor('#EEE');
tooltipObj.setCloseMessage('Exit');
tooltipObj.initFormFieldTooltip();
</script> 
harishtps
  • 1,439
  • 7
  • 20
  • 35