Subscribe to our RSS

How to Add Hints & Auto Focus Form Fields using jQuery

Tags: , , ,

How to Add Hints & Auto Focus Form Fields using jQuery

This tutorial teaches you how to easily add hints to your forms and auto focus fields.

We are continuing a series of quick articles for web developers which explain how to automate things during website programming. Today we will talk about forms and two usability items the every current website should have:

  • Auto Focus Fields. The first field in the form should be focused by default, so that the user can start entering his password or name right after page loaded and would not have to click the field to start typing. This is a small and obvious usability items, but still a very important one.
  • Hints. Forms should have quick tips for fields which requires entering data in a specific format (for example email address, phone number, date / time, etc.)

So, let’s say our website have this contact form:

screen-1

Auto Focus

Full Name – this field will be automatically focused when the page loads, here is how it will be coded in the markup:

<input type="text" name="fullname" id="fullname" class="auto-focus" />

Notice the field has ‘auto-focus’ class. This is so that any field in the form with this CSS class will be automatically focused on page load.

The auto focus can be implemented in JavaScript:

$('.auto-focus:first').focus();

2.) Email Address (the same for phone number and message fields) – If the field does not contain any value it will show a quick tip on the required data format. When the user clicks the field, the tip will disappear and let the user enter data. Here is how it will look in the markup:

<input type="text" name="email" id="email" title="i.e. me@example.com" class="auto-hint" />

As you see, our hint will be stored in ‘title’ attribute of the field. This will let user see it when the field already contains a value in it.

By default, the color of text in input field will be black, we need to differentiate the color of the hint and common input value. Let’s set this color in CSS:

<style type="text/css">
    .auto-hint { color: #AAAAAA; }
</style>

Now we move to JavaScript implementation. At first we should display the hint in fields which have empty values:

$('INPUT.auto-hint, TEXTAREA.auto-hint').focus(function(){
    if($(this).val() == $(this).attr('title')){
        $(this).val('');
        $(this).removeClass('auto-hint');
    }
});

And handle focus / blur events:

$('INPUT.auto-hint, TEXTAREA.auto-hint').blur(function(){
    if($(this).val() == '' &amp;&amp; $(this).attr('title') != ''){
       $(this).val($(this).attr('title'));
       $(this).addClass('auto-hint');
    }
});
$('INPUT.auto-hint, TEXTAREA.auto-hint').each(function(){
    if($(this).attr('title') == ''){ return; }
    if($(this).val() == ''){ $(this).val($(this).attr('title')); }
    else { $(this).removeClass('auto-hint'); }
});

That’s it! ;) Click the Demo button to see it in action.

Screenshots

rp

June 18, 2010

$(’.auto-focus:first’).focus();

?

what is this? It appears you neglected to type the complete code. If you aren’t going to do it right, don’t do it at all.

ajaxBlender.com

June 23, 2010

rp,

From the tone of your comment, I can see you must be a valuable addition to any online community. Welcome to ajaxBlender! :p

Anyway, you probably didn’t realize that this selector is done in jQuery. The code will find the first input field with the ‘auto-focus’ class and focus it, so that the user will not have to click the field with the mouse to start typing. This method is a MUST for a user-friendly web page containing forms.

Thank you for your response, though.

Alex

Peter Mounce

March 11, 2011

Hey – thanks for the snippet – simple and to the point and precisely what I needed at short notice.

Mitziman

March 16, 2011

hey,

your jquery snippets got mixed up a little.

Phil

April 20, 2011

Couple of issues with this:

1. You’ve got & & in your source code which should obviously be a logical and.
2. When submitting the form, if any of the fields have not been filled they will contain the default text. A bit of extra JS is needed to remove it on submission.

Phil

April 20, 2011

And by & & I meant:

& &

Or if that doesn’t show then HTML entity escaped ampersands.

Phil

April 20, 2011

Here is how to clear the fields that have the title set as the content. Just make sure your title can’t be a possible value a user might enter:

$(’form’).submit(function(){
$(’.auto-hint’).each(function(){
if($(this).attr(’title’) == $(this).val()){
$(this).val(”);
}
});
});

Thomas

May 2, 2011

Thanks for this helpful script, I have used it 3 times already. Also a classy way to handle someone so rude. I will follow this blog because of that.

Andrew

June 24, 2011

Useful script, thanks.

Jeff

August 18, 2011

I have a CMS that uses mootools and when I add the handle focus / blur events, I get JavaScript errors and I can’t figure out how to fix.

I added the noConflict function but I still get the errors.

Here is my code:

{literal}
//no conflict jquery
jQuery.noConflict();
//jquery stuff
(function($) {
$(’.auto-focus:first’).focus();
$(’TEXTAREA.auto-hint’).focus(function(){
if($(this).val() == $(this).attr(’title’)){
$(this).val(”);
$(this).removeClass(’auto-hint’);
}
});
$(’TEXTAREA.auto-hint’).blur(function(){
if($(this).val() == ” && $(this).attr(’title’) != ”){
$(this).val($(this).attr(’title’));
$(this).addClass(’auto-hint’);
}
});
})(jQuery);
{/literal}

Here are the errors:


Message: Expected ‘)’
Line: 628
Char: 31
Code: 0
URI: http://www.mysite.com/mypage.php

Message: Object doesn’t support this property or method
Line: 1
Char: 20118
Code: 0
URI: http://www.mysite.com/js/mootools12-min.js

Message: Object doesn’t support this property or method
Line: 65
Char: 5
Code: 0
URI: http://www.mysite.com/mypage.php

In my head tag, the script include for mootools comes before the jquery include. I can’t change that because that would mean I would have to add the jquery include to the header template which would add jquery to every web site page. I only want jquery on this particular page so I used a Smarty capture to add the jquery include to the head tag of this page.

Anyone have any ideas?

Jd

October 26, 2011

this is not good code, since it inputs the title as a Value and gets submitted

Leave a comment

Your name

February 7, 2012

Spam protection. Enter result of expression you see in the box below.

2 + 2 =