Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an HTML form with pre-filled in "instructions" that clear when a user clicks in the box?

Tags:

html

I have an HTML form as follow:

   <form method="POST" action="http://">
 Username: <input type="text" name="username" size="15" />
 Password: <input type="password" name="password" size="15" />
<input type="submit" value="Login" />
 </div>
 </form>

I would like functionality such that the text fields have instructions in them that clear when a user clicks in the box so that I can save space and remove the words Username and Password from outside the forms.

How can this be achieved?

like image 665
Melanie Shebel Avatar asked Apr 27 '11 09:04

Melanie Shebel


People also ask

How do you clear input after submitting a form?

To clear an input field after submitting:Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.

What type of HTML form element is used for clearing the form?

The <input type="reset"> defines a reset button which resets all form values to its initial values.

How do I create a clear button in HTML?

Type the <input type="reset"> tag into the code towards the top and/or bottom of the HTML form. Close the form after all input fields are entered with a final </form> tag. Save and preview your new adjusted form with the new reset button.


2 Answers

The feature you're looking for is called a "placeholder". (if nothing else, just knowing this term will help you search for more info in Google)

In modern browsers which support HTML5, this is a built-in feature which you can use very easily, as follows:

<input type='text' name='username' size='15' placeholder='User name' />

However, this method only works with up-to-date browsers which support this feature.

Older browsers will need to have some Javascript code to do it. Fortunately, there are a number of scripts you can use, including some written as JQuery plug-ins. The ones I'd recommend are those which tie into the placeholder attribute on the input field, so that you can support it natively in the browsers which have this feature and fall-back to Javascript for those that don't.

Try this one: http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html

like image 188
Spudley Avatar answered Nov 10 '22 16:11

Spudley


If you use HTML5 you can do this using the placeholder attribute:

<form method="POST" action="http://">
    <label for="username">Username:</label> 
    <input placeholder="Username" id="username" name="username" size="15">
    <label for="password">Password:</label> 
    <input placeholder="Password" type="password" id="password" name="password" size="15">
    <input type="submit" value="Login">
</form>

I'd still include the Username and Password text wrapped in <label> tags for accessibility, but you could always hide them with some CSS like this:

form {
    position:relative;
}
label {
    position:absolute;
    top:-9999px;
}
like image 39
Ian Oxley Avatar answered Nov 10 '22 16:11

Ian Oxley