Default Value

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)

Permalink / Subscribe / Leave comment (11)

Comments

Author Aarron Walter
Posted May 13, 2008

A simple, elegant solution. Thanks for sharing.

Author Aamir Afridi
Posted July 8, 2008

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

Author Rob Curry
Posted July 14, 2008

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!!

Author bo
Posted April 16, 2009

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

Author San
Posted September 14, 2009

Nice plugin. Only issue was that it displayed the default prompt even when a value existed for an element. So here is the modified code – works for me..hope it helps someone else.

(function($) {

$.fn.defaultvalue = function() {

// Scope
var elements = this;
var args = arguments;
var c = 0;

return(
elements.each(function() {

// Default values within scope
var el = $(this);
var elval = el.val();
var def = args[c++];
var wdef=(elval ==''?def:elval);

el.val(wdef).focus(function() {
if(el.val() == def) {
el.val("");
}
el.blur(function() {
if(el.val() == "") {
el.val(def);
}
});
});

})

);
}
})(jQuery)

Author Benjibuls
Posted October 21, 2009

Great plugin. Added a little code to set and reset a style so that the default-value content can be presented differently from real input:

(function($) {

$.fn.defaultvalue = function() {

// Scope
var elements = this;
var args = arguments;
var c = 0;
var defaultclass = “default-value”;

return(
elements.each(function() {

// Default values within scope
var el = $(this).addClass(defaultclass);
var def = args[c++];

el.val(def).focus(function() {
if(el.val() == def) {
el.val(“”);
el.removeClass(defaultclass);
}
el.blur(function() {
if(el.val() == “”) {
el.val(def);
el.addClass(defaultclass);
}
});
});

})
);
}
})(jQuery)

Author Eric Falsken
Posted November 24, 2009

I made this defaultvalue script auto-bind to the “defaultvalue” attribute by adding this to the top of my page:

function defaultvalue_init(s) {
if (s == undefined) s = ‘input[default],textbox[default],input[defaultvalue],textbox[defaultvalue]‘;
$(s).each(function(i) {
$(this).defaultvalue($(this).attr(‘defaultvalue’));
});
}

$(document).ready(function() {
defaultvalue_init();
});

with this, you can write code like:

The reason I put the defaultvalue_init method outside of the document.ready() handler was so that I can call it after an AJAX request might modify the page body.

Author Eric Falsken
Posted November 24, 2009

(editing my previous post so the tags show up)

I made this defaultvalue script auto-bind to the “defaultvalue” attribute by adding this to the top of my page:

<script type=”text/javascript”>
function defaultvalue_init(s) {
if (s == undefined) s = ‘input[default],textbox[default],input[defaultvalue],textbox[defaultvalue]‘;
$(s).each(function(i) {
$(this).defaultvalue($(this).attr(‘defaultvalue’));
});
}

$(document).ready(function() {
defaultvalue_init();
});
</script>

with this, you can write code like:
<input type=”text” defaultvalue=”put something here” />

The reason I put the defaultvalue_init method outside of the document.ready() handler was so that I can call it after an AJAX request might modify the page body.

Author ilkinulas
Posted December 12, 2009

Elegant solution, thanks for sharing.

I changed your plugin a bit. I am adding a class to default values and removing that class when a text is entered.


el.addClass(‘default_form_value’);

el.removeClass(‘default_form_value’);

Author 10 Jquery Form Tips | JktAgusWebID
Posted December 16, 2009

[...] 09. Default Value [...]

Author ABCoder
Posted March 8, 2010

I need to use it with validator plugin. How do I do that ?

Leave a comment