Search 
DailyCoding > Scripting

"Default Text" Fields Using Simple jQuery Trick

Explains how to implement default text input field using simple jQuery trick
Author admin on Aug 1, 2008 8 Comments
Rate it    (Rated 4 by 14 people)
3,409 Views

A lots of the time you want to add instructions to text box fields of the page to provide usability information to the users. Alternate way of doing this could be using default text in the input fields. A default text is the text which appears in the text box when it is empty. e.g.

Name
Email
Website
Comment

You can see this is useful and good idea to have this in the UI. There is an easy trick in jQuery to do this. Below is the step by step procedure to do this.

Step1

If you don't have jQuery then download latest from its website and include in you page.

<script src="jquery.js" type="text/javascript"></script>

Step 2

Create two CSS classes defaultText and defaultTextActive as mentioned below.

<style media="screen" type="text/css">
    .defaultText { width: 300px; }
    .defaultTextActive { color: #a1a1a1; font-style: italic; }
</style>

Step 3

Add this JavaScript to you page

<script language="javascript">
<!--
$(document).ready(function()
{
    $(".defaultText").focus(function(srcc)
    {
        if ($(this).val() == $(this)[0].title)
        {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });
    
    $(".defaultText").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });
    
    $(".defaultText").blur();        
});
//-->
</script>

Step 4

To every text field or text area where you want to apply default text add "defaultText" as css class and specify the default text in the title attribute. e.g.

<input class="defaultText" title="e.g. someone@example.com" type="text" />

You default text setup is ready. Run the page and see the magic. Click here for a running sample page.

CSS | Html | Javascript | jQuery | Utility

Discussion

Bill On Aug 2, 2008 12:34 PM
What would you suggest doing in a case where the field might not be filled by the user. Currently the text that is being put in the title attribute is posted back? I would imagine there has to be a way using jquery to remove it on submit, but this is my first day looking into what is possible with jquery.

Bill On Aug 2, 2008 12:43 PM
I just found this plugin that removes the text prior to submit: http://plugins.jquery.com/project/ToggleFormText. I found it from a link in a Matt Berseth article http://mattberseth.c....xtboxwatermark.html.

SeanJA On Aug 3, 2008 10:08 AM
I think the code would probably look something like:

onSubmit(foreach(".defaulttext"){
if (title of field = content of field){
content of field = ""
}
}

I don't really know javascript, but it can't be that difficult to figure out.

SeanJA On Aug 3, 2008 10:09 AM
Awww... it lost its formatting...

SeanJA On Aug 3, 2008 10:11 AM
And looking at the plugin, that is exactly what it was...

Alex On Aug 13, 2008 01:58 AM
To clear the default text on submit:

<pre>
$("form").submit(function() {
$(".defaultText").each(function() {
if($(this).val() == this.title) {
$(this).val("");
}
});
});
</pre>

Daily Coder On Aug 13, 2008 05:20 AM
Alex, thanks for the solution.

Vaibhav Gupta On Sep 12, 2008 01:08 AM
$("form_name").submit(function() {
$(".defaultText").each(function() {
if($(this).val() == $(this)[0].title) {
$(this).val("");
}
});
});

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved