Default Value

A simple Javascript-based user enhancement to your web forms

The default value JQuery plug-in has been developed as a progressive enhancement for input and textfield elements. Allowing an initial value to appear within the form element, acting as a prompt for the user. This value is removed upon element focus and returned (on blur) if an alternate value has not been entered the user.

Download Javascript source file (jquery.defaultvalue.js) or example pack (.zip)

Examples

To apply the plug-in to an input element within a page:

jQuery(function($) {
    $("#input-one").defaultvalue("Your text");
});

To apply default values to multiple form elements (either input or textarea), each with a different value:

jQuery(function($) {
    $("#input-one, #input-two, #textarea").defaultvalue(
        "Input text one",
        "Input text two",
        "Textarea text"
    );
});

Download Javascript source file (jquery.defaultvalue.js) or example pack (.zip)

Read

Comments

Comment by Aarron Walter on May 13th, 2008 at 4:52 pm

A simple, elegant solution. Thanks for sharing.

Comment by Aamir Afridi on July 8th, 2008 at 1:58 am

nice one. i am using it for my search box…

Comment by Rob Curry on July 14th, 2008 at 5:41 am

This is a neat little plug-in but i had to adapt it slightly as it set the prompt even if the box already had a value. I need the prompt to be there but only if the user hasn’t already given an answer. Thanks for a great, simple, plugin though!!

Comment by bo on April 16th, 2009 at 12:50 pm

Couple of things:
1. When using tabs and say adding default value to two textareas with same class then plugin fails since it has ++c counter which would hit “undefined” for the second textarea
2. Should have some sort of option and callback so when you submit form it would remove default value (I don’t want send out “- serach by ID -”)
3. How do you handle form validation when “defaultvalue” is used as a hint and not an actual value? JQuery Validator for example

Want to comment?





Submit

Recent comments

  1. bo Couple of things: 1. When using tabs and say adding default value to two t...
  2. Rich I took a similar course at uni, and came across this interesting book... h...
  3. Cihat Hi good work thanks ...
  4. Matthew Cieplak This works great, except that it makes any pngs that happen to be hidden on...
  5. bahman Hello Dear OBAMA I am an iranian, I am very interested to you. I wish y...
Find