Profile image for Patrick Sparrow psparrow
Sets a field's default value if it's empty. Clears the field on focus. Resets the default on blur.
Language
JavaScript
Tags
default field form javascript jquery

Default field values with jQuery

1 function setupField(el, value) { 2 if(el.val() == '') el.val(value); 3 el.focus(function() { if(el.val() == value) el.val(''); }) 4 .blur(function() { if(el.val() == '') el.val(value); }); 5 } 6 7 // usage: 8 setupField($('#email'), 'Enter your email address');

Comments