Default Text Label in Textbox using JavaScript/jQuery

      

html-textbox-label-text
In one of my previous article about Change Form Textbox Style on Focus, we discussed about Usability and HCI issues. Slight change in web pages/input forms can increase user experience.

Nowadays, it has became a common practice to give user labels on fields that they are editing. For example, if you see Google’s custom search textbox in any website, most of them will have a Google’s Logo in background and when user clicks on Textbox, the logo disappears. Thus we can create an input form with labels in background of textboxes. When user select the textboxes, the labels disappear enabling user to edit the textbox.

textbox-background-text-vanish

Let us see how can we use JavaScript/jQuery to achieve this.

Step 1: The HTML

We will use following simple HTML Form. Create an HTML file and Copy and paste following code.

<FORM method="post">
<TABLE>
<TR>
	<TD>Username</TD>
	<TD><INPUT type="text" value="" name="username" title="Enter Username"/><TD>
</TR>
<TR>
	<TD>Age</TD>
	<TD><INPUT type="text" value="" name="username" title="Enter Age"/><TD>
</TR>
<TR>
	<TD>City</TD>
	<TD><INPUT type="text" value="" name="username" title="Enter City"/><TD>
</TR>
<TR>
	<TD>Comment</TD>
	<TD><INPUT type="text" value="" name="username" title="Enter Comment"/><TD>
</TR>
</TABLE>
</FORM>

Notice that while defining each text fields, we have provided title attribute. The text that is displayed on background of textbox is taken from the Title attribute. So generally the title attribute should be like “Enter Email Address” or “Type Address”.

Step 2: The CSS

Copy following CSS in your HTML file. You may want to include a separate CSS file for this.

.text-label {
	color: #cdcdcd;
	font-weight: bold;
}

The above CSS class is applied to the Textbox when user has not entered anything in it. Once the user starts typing in Textbox the CSS class is removed.

Step 3: The JavaScript

We are almost done with our implementation. Only thing left is the JavaScript code that actually add and remove css to textboxes.

$('input[type="text"]').each(function(){

	this.value = $(this).attr('title');
	$(this).addClass('text-label');

	$(this).focus(function(){
		if(this.value == $(this).attr('title')) {
			this.value = '';
			$(this).removeClass('text-label');
		}
	});

	$(this).blur(function(){
		if(this.value == '') {
			this.value = $(this).attr('title');
			$(this).addClass('text-label');
		}
	});
});

The above code is straight forward. For each textbox in the webpage, we hook handler function to events focus and blur. When Textbox is focused, check if the value is same as Title. If so, then remove the background label css and let user edit the value in textbox. And while blur event(focus lost), check the value: if user has not entered anything, set the background css again.

Online Demo


Online Demo


Facebook  Twitter      Stumbleupon  Delicious
  

6 Comments on “Default Text Label in Textbox using JavaScript/jQuery”

  • nick wrote on 17 October, 2009, 10:17

    hi viral..
    i use asp.net and want to try your code..
    when i run the code, a error message appears “Microsoft JScript runtime error: Object expected”

    $('input[type="text"]').each(function() {
    	this.value = $(this).attr('title');
    	$(this).addClass('text-label');
    	$(this).focus(function() {
    		if(this.value == $(this).attr('title')) {
    			this.value = '';
    			$(this).removeClass('text-label');
    		}
    	});
    
    	$(this).blur(function() {
    		if(this.value == '') {
    			this.value = $(this).attr('title');
    			$(this).addClass('text-label');
    		}
    	});
    });
    

    plz help me

  • Viral Patel wrote on 17 October, 2009, 13:42

    Hi Nick,
    The code looks fine to me. The error you are getting is because some object has not been initialized properly. Can you confirm if you have included jQuery javascript file in your code?
    You must have something like following in your html:

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    

    You can do one thing. Take a look at source code of Online demo and see if you are missing anything in your code.

  • Mark Kadlec wrote on 19 November, 2009, 1:22

    Very nice Viral! I’m surprised that asp.net doesn’t have this type of control functionality.

  • ben wrote on 18 January, 2010, 23:55

    This is a good start, but could use some polishing. I added a check to see if the title attribute exists for field and added an onsubmit check to be sure we don’t submit default text.

    It’d also be nice to use it on textareas, which shouldn’t be too hard.

  • vikas wrote on 20 February, 2010, 2:23

    Hi, Viral
    It’s working fine with the form that load with in the same page but when I’m loading a form through Ajax in a particular div tag t his won’t work with the dynamically loaded forms.
    Is their any way to works also with dynamically loaded form?

  • James wrote on 24 February, 2010, 17:42

    Hi,

    Great! It is straightforward. I just wonder why it doesnt work if I am putting the script on an external source or even putting it on the tag. It only works when the script is positioned before the tag.

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 ViralPatel.net. All rights reserved.