Default Text Label in Textbox using JavaScript/jQuery

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.

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.

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.

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.

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:

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?

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.

Hey everyone, here is one I made that handles Password inputs as well, and also ignores text boxes with no Title attribute. You can’t change input types dynamically with all browsers so instead I show and hide two different inputs. You need one with ID Password and another with ID PasswordDummy, For clients with no javascript, best to set PasswordDummy to display:none initially.

Enjoy.

[CODE=”js”]

$(‘input’).each(function() {

if (this.id == ‘Password’) {
// Handle Password differently – it only gets an onblur, in which it gies invisible and activates the PasswordDummy if it is empty

Hi,
I’am a asp.net user and I want to say someting about your code.Firstly I appreciate you for your simple and chic code.I have been using it very well.But I want to add some missing code(according to me :)).My Code :

Of Course that’s your code,not mine.But I added oneline,why?
After the same page loading, the value of textbox continue to stand in the textBox.What will be doing is to prevent this problem. So now, after the page loading your code works very well!

Won’t this be an issue when you need to validate required fields, though? If the user submits a form without entering anything, it will submit that default text as the value, so then it won’t properly recognize that the user didn’t fill out the required field. How do you get around this?

Hi Prateek,
jQuery does provide live events which can be used to map events to any current and future element, but unfortunately live() does not provide any create method which can be used to handle new element creation. So for now the simplest solution (without adding any additional plugin) would be to wrap above javascript in some function() { } and call it everytime a new element is added.

So you cannot put the javascript in the header section or else it won’t work. Javascript has to be below the form for this to work. It wasn’t working for me at first then i checked the source of online demo and saw that javascript was at the end after Is there any way to make this work by having javascript in the header?

hai friend do u have code for watermarking text box using ajax in jsp ma…pls reply dawhich is i will entermy emp id if pan is there….in database i want the pan and dob text box to be enabled..which is disabled state….otherwise…dob and doj have to be enabled