Meters
Home     Frequency Meter   Resistance Meter   Potentiometer   Voltmeter Meter   8051 frequency counter   rf frequency counter   portable frequency counter   word frequency counter   agilent frequency counter   pic frequency counter   ground resistance meter   All Tags
Home ›› Frequency Meter ›› Javascript : Creating Textbox watermarks
   
 You may find:
word frequency counter fatal error ; word frequency counter clipboard button ; word frequency counter Technical Notes
 Popular Tags:
word frequency counter text file | word frequency counter artificial intelligence | word frequency counter rank | word frequency counter clipboard button | word frequency counter textbox | word frequency counter web design | word frequency counter fatal error | hermetic word frequency counter | Words | High Frequency Words
Javascript : Creating Textbox watermarks

Tag:word frequency counter textbox word frequency counter | 50 Viewers| sanketvasa 2008-09-21 04:36:27 Publish:

One of the common requirements when working with web page forms is to have some default text show within a text box and make it disappear when the user enters something in the text box, in other words, create a text box watermark.

Now if you are working with ASP.NET then an easy way to do this is to use the AJAX Control Toolkit's 'TextboxWatermark' control and just link the AJAX control to the text box in which we want the watermark to appear. The AJAX control provides a number of options to control the watermark. However, if you are working with pure HTML pages then, you are stuck with Javascript and need to figure out a way to control the watermarking. I recently wrote some code to do the same and would like to share it in this post.

Assuming you have a html page added to your project, the following steps would enable the watermarking of the text box :

  1. The first step obviously is to define the basic HTML tag which will add a normal text box to your page. Here's a sample code for this :

    <input id="txtUsername" name="txtUsername">

  2. The next step is to define a variable which will store the string that will act as the watermark (Thanks Pratik for sharing this best practice with me). The advantage of creating a variable is that it makes it easy to perform comparisons (which will be mentioned in a later step) and also requires you to change the watermark text in only one place.

    var gcUsernameTbDefaultText='Please enter a username';

  3. The trick to have the watermark appear and disappear is to capture the 'onblur' and 'onfocus' events and manipulate the contents of the text box. The following code snippet shows functions being called for these 2 events.

    <input type="text" id="txtUsername" name="txtUsername" onfocus="clearTextbox('txtUsername',gcUsernameTbDefaultText);" onblur="showDefaultText('txtUsername',gcUsernameTbDefaultText);" />

    As you can see, the functions called when the events occur are passed the id of the text box on which the watermark is needed and also the watermark text, making these functions generic and usable with any text box.

  4. The last and final step is to implement the 2 functions that do the actual manipulation. So here they are :

  5. function clearTextbox(objId,text)
    {
    try
    {
    var obj = document.getElementById(objId);
    //Clear the textbox only if the textbox contains the original default value
    if (obj != null && obj.value.toString().toLowerCase() == text.toLowerCase())
    {
    obj.value = "";
    }
    }
    catch (e)
    {
    alert(e.message);
    }
    }

  6. function showDefaultText(objId,text)
    {
    try
    {
    var obj = document.getElementById(objId);
    if (obj != null && obj.value == "")
    {
    obj.value = text;
    }
    }
    catch (e)
    {
    alert(e.message);
    }
    }

So there you have it, simple text box watermarking. There are 2 improvements one can do on this implementation :

  1. Dynamically apply styles in order to show the watermark text in gray, with some sort of transparency and have the normal text with normal appearance
  2. This approach won't work with 'password' text boxes since even the watermark will appear as asterisks. This post talks about implementing watermarks for password text boxes
Yahoo: word frequency counter Javascript : Creating Textbox watermarks word frequency counter textbox
Google: word frequency counter textbox Javascript : Creating Textbox watermarks word frequency counter
Post your comment about:    Javascript : Creating Textbox watermarks

   

Topics: Frequency Meter Resistance Meter Potentiometer Voltmeter Meter
All Contents was collected by orders.If you have any problem please Click Me © 2008 Meters Time 0.083789 S(s).Query 5.