This article will take a look at the functionality provided by the Placeholder attribute in HTML5 and also the possible customizations that can be applied to it. Problem Definition The requirement is a functionality that is provided in most of the current day websites. Web sites give a helpful pointer text in the textboxes where input is required. Typically the search boxes in most web sites have a gray text hint to enter the search term. When the user clicks on the textbox, the search term vanishes and the user can type in their input, knowing what is expected. This has been done historically using javascript and the onfocus event handlers. When the element gains focus, a script function runs, to hide the hint text and when the focus is moved out, a script function to display the text again, is executed. Solution HTML5 includes a placeholder attribute which allows you to declaratively specify this temporary text. This is typically a short length text. It's a quick and easy solution for a functionality that's very widely used. Do note that the placeholder should not be used to replace labels. It's purpose is to provide additional information that is transient in nature but not a label replacement. The recommendation is to use shorter messages - In case you need to use longer hints/descriptives, you can consider using the title attribute to provide a tooltip instead. Syntax<INPUT type=text name=FirstName placeholder="First Name"><INPUT type=text name=FirstName placeholder="Last Name"> You cannot add other html content - just text can be specified within the placeholder. However, you can provide some css - the extensions are vendor specific and are discussed further below in the article. How does it look?Here is a view of the sample run in FirefoxFigure 5: Firefox running a page with the html5 input placeholder When the First Name text box is clicked, note the placeholder text has dissapeared.Figure 5: Firefox running a page with the html5 input placeholder - the placeholder dissapears when the control has focus. Source Code: save as testph.htm
<!DOCTYPE HTML> <INPUT type=text name=firstname placeholder="First name"><INPUT type=text name=lastname placeholder="Last name"><input type=button name=submit value="Search"/>
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: