Latest tutorial: Making a Movieclip face another Movieclip or point on the stage | Ask Tutorial5!
 

Build Your Own Database Driven Website Using PHP & MySQL

SitePoints Best Seller book on scripting.

  • Learn how to install and administer PHP & MySQL on Windows, Linux or Mac

  • Build your very first live Database Driven Website using PHP & MySQL

  • Instantly apply working code examples from the book to your Website


Download the free chapters now!

Input form text switch using JavaScript

(10 votes)
Written by DanielRo   
In this tutorial we are presenting a very simple and efficient method of providing extra information in form text fields using simple JavaScript.

When building forms you will always find a need to provide more and more information and guidelines regarding the type and form of data you want users to submit by using the bare minimum of code and ease of mind.

By using JavaScripts onFocus and onBlur event handlers one can provide text in text fields that automatically disappears when the user is editing that field.

Input form text switch using JavaScript example

Here is the actual code snippet:

<input type="text" name="searchbox" value="Enter search phrase" onfocus="if (this.value == 'Enter search phrase') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter search phrase';}" />
<input type="button" value="Go" />


This is typical code sentence for a input type text field except for the extra JavaScript.

value="Enter search phrase"
This is the place were the default input text is declare, "Enter search phrase" will be auto typed into the input text field.

onfocus="if (this.value == 'Enter search phrase') {this.value = '';}"
When the user clicks on the text field the script checks to see if the value of the text field is "Enter search phrase". If the values match value="Enter search phrase" and (this.value == 'Enter search phrase') then the script resets the text field default value to nothing {this.value = '';}.

onblur="if (this.value == '') {this.value = 'Enter search phrase';}"
When the user clicks outside of the text field the script checks to see if the default value has been modified (if the user entered information). If the user did not enter any info and the text field is blank, then the script resets the default text field value to Enter search phrase {this.value = 'Enter search phrase';}.

To get this working on your forms all you need to do is copy this code and replace the three instances of Enter search phrase with whatever you want your text fields to display.


Subscribe now via RSS feed and get all the new tutorials

written by Johan , March 07, 2008

Thanks!! :D

I've searched in 2 hours for this, and finally found it. :)

- Johan

Do you need more help? Ask now!
 

busy
Last Updated ( Sunday, 21 October 2007 )