Saturday, January 17, 2009

jQuery Form Input Hints Plugin

Use this script to automatically display hints on input textboxes in your forms. It uses the amazing jQuery library. I converted the script into a jQuery plugin called Form Input Hints to make it more flexible.

Update: I created an open source project on CodePlex to host the project. You can download the source with a demo there. I encourage anyone who would like to add to the functionality or change the code to do so in the project & commit your changes. I'll monitor the activity and create releases after testing.

Update: Now when the hint text is active, it adds a CSS class called “hint” to the textbox so you can style your hint text.

Check it out:

Usage:

<input type="text" title="Hint Text!" />

$(document).ready(function() {
   $('input[title]').inputHints();
});

Download from CodePlex: http://jqueryhints.codeplex.com (includes minified, debug, & demo)

Here is the Input Hints jQuery Plugin code:

// jQuery Input Hints plugin
// Copyright (c) 2009 Rob Volk
// http://www.robvolk.com

jQuery.fn.inputHints=function() {
    // hides the input display text stored in the title on focus
    // and sets it on blur if the user hasn't changed it.

    // show the display text
    $(this).each(function(i) {
        $(this).val($(this).attr('title'))
            .addClass('hint');
    });

    // hook up the blur & focus
    return $(this).focus(function() {
        if ($(this).val() == $(this).attr('title'))
            $(this).val('')
                .removeClass('hint');
    }).blur(function() {
        if ($(this).val() == '')
            $(this).val($(this).attr('title'))
                .addClass('hint');
    });
};

21 comments:

  1. This is very well done. Thank you for the hard work!

    ReplyDelete
  2. I tried loads of others and this just works, the others didn't, and it's the simplest script too!

    Thanks so much

    ReplyDelete
  3. A simple solution. Thank you very much.

    ReplyDelete
  4. Brilliant! Super simple and light weight.

    THANKS!!!

    ReplyDelete
  5. I have taken the liberty to making a slight mod to your plugin (which works quite nicely - thanks) to provide namespace protection. $ conflicts with Prototype.

    // jQuery Input Hints plugin
    // Copyright (c) 2009 Rob Volk
    // http://www.robvolk.com
    (function($){$.fn.inputHints=function(){$(this).each(function(i){$(this).val($(this).attr('title'));});$(this).focus(function(){if($(this).val()==$(this).attr('title'))
    $(this).val('');}).blur(function(){if($(this).val()=='')
    $(this).val($(this).attr('title'));});};})(jQuery);

    ReplyDelete
  6. Wonderful. So simple, thanks :)

    ReplyDelete
  7. I created an open source project for my jQuery Form Input Hints plugin so we can keep it alive and add features. I'll merge the contributions in the blog comments as well.

    http://jqueryhints.codeplex.com

    ReplyDelete
  8. just posted my code in a discussion on codeplex. The code will remove the hint before the form is submitted. also adds some spaces to the title just in case someone tries to enter the exact title text.

    ReplyDelete
  9. Thanks Rob...

    I had modified a bit to include some color things and select the text. Below is the javascript code:

    // show the display text
    $(this).each(function(i) {
    $(this).val($(this).attr('title'));
    }).addClass('blur');

    // hook up the blur & focus
    return $(this).focus(function() {
    if ($(this).val() == $(this).attr('title'))
    $(this).val('').removeClass('blur').addClass('focus');
    else
    $(this).select();
    }).blur(function() {
    if ($(this).val() == '')
    $(this).val($(this).attr('title')).removeClass('focus').addClass('blur');
    });


    and this the css code:
    <style type="text/css">
    input.blur {
    color: #999;
    border: 1px ridge #c0c;
    }

    input.focus {
    color: #000;
    border: 1px ridge #c0c;
    background-color:#ffc;
    }
    </style>

    ReplyDelete
  10. thanks! would you mind submitting your changes to the project via CodePlex? http://jqueryhints.codeplex.com/

    ReplyDelete
  11. demo doesn't appear to work? no hint text shown in field "Check it out"

    ReplyDelete
  12. Hello Rob...
    I´ve made some modifications to the script.
    I put it here: http://jqueryhints.codeplex.com/Thread/View.aspx?ThreadId=213075
    Thanks!

    ReplyDelete
  13. nice

    http://java.pakcarid.com//default.aspx?sub=25&Sls=25

    ReplyDelete
  14. I made a slight modification, which removes the hint when the form is submitted.

    $(document).ready(function() {
    //Shows the title in the text box, and removes it when modifying it
    $('input[title]').each(function(i) {
    $(this).val($(this).attr('title'))
    .addClass('hint');

    $(this).focus(
    function() {
    if ($(this).val() == $(this).attr('title')) {
    $(this).val('')
    .removeClass('hint');
    }
    }
    );

    $(this).blur(
    function() {
    if ($(this).val() == '') {
    $(this).val($(this).attr('title'))
    .addClass('hint');
    }
    }
    );
    });

    //Clear input hints on form submit
    $('form').submit(function() {
    $('input.hint').val('');
    return true;
    });
    });

    ReplyDelete
  15. // show the display text
    $(this).each(function(i) {
    ** if($(this).val() == '')
    $(this).val($(this).attr('title'))
    .addClass('hint');
    });

    start value...

    ReplyDelete
  16. ITs not working....... :'( :'(

    ReplyDelete
  17. How could you use this for a textarea instead of a textbox?

    ReplyDelete
  18. It should work the same. does this not work?

    $('textarea[title]').inputHints()

    ReplyDelete