Form Hints 1.0

Created Tuesday 16th of September 2008 by Andreas Lagerkvist
Copyright © 2008 Andreas Lagerkvist (andreaslagerkvist.com)

What it Does

Using this plug-in you can add a descriptive hint to any form-control you may have on your site.
Add a hint by giving the form-control a title-attribute. The hint will disappear and reappear as the user clicks the control.

How to Use

jQuery(document.body).formHints(); would give every form-control with a title-attribute a hint.

Run the plug-in on a parent-element of the form-controls you want to affect. If you run it on document.body every form-control with a title-attribute will get a hint.

Example

Example Code

HTML

<div id="jquery-form-hints-example">
<
form method="post" action="">

    <
p>
        <
label>
            
Dummy<br />
            <
input type="text" name="dummy" title="E.G. Dummy" />
        </
label>
    </
p>

    <
p><input type="submit" value="Ok" /></p>

</
form>
</
div>

JS

// I'm not actually running it because my site already uses formHints
// jQuery('#jquery-form-hints-example').formHints();

Source Code

jQuery.fn.formHints = function (conf) {
    var 
config jQuery.extend({
        
formControls:    'input[title], textarea[title]',
        
className:        'default-value'
    
}, conf);

    return 
this.each(function () {
        
jQuery(config.formControlsthis).each(function () {
            var 
jQuery(this);

            
t.formHint t.attr('title');
            
t.attr('title''');

            if (
t.val() === '' || t.val() == t.formHint) {
                
t.addClass(config.className).val(t.formHint);
            }

            
t.focus(function () {
                if (
t.val() == t.formHint) {
                    
t.val('').removeClass(config.className);
                }
            });
            
            
t.blur(function () {
                if (
t.val() === '' || t.val() == t.formHint) {
                    
t.addClass(config.className).val(t.formHint);
                }
            });
        });

        
// Remove hints on form submission
        
jQuery('form'this).submit(function () {
            
jQuery('.' config.classNamethis).removeClass(config.className).val('');
        });
    });
};

Download

Plug-in

Requires

blog comments powered by Disqus

Random jQuery Plug-ins

  • Live Search

    Use this plug-in to turn a normal form-input in to a live ajax search widget. The plug-in displays any HTML you like in the results and the search-res...

    Details

  • Static to Dynamic Google Map

    This plug-in turns a static google map into a dynamic one by inspecting the attributes passed to the static map API. It's still in beta and doesn't su...

    Details

  • Center

    This little pluggy centers an element on the screen using either fixed or absolute positioning. Can be used to display messages, pop up images etc.

    Details

More Plug-ins

Recent Comments

Powered by Disqus
Page cached. Loaded in: 0.0067 second(s).
Last DB change: 2012-04-02 11:06:05
Last file change: 2012-04-25 20:30:39
Cache created: 2012-05-18 11:12:38