/** * Default value handler based on jQuery * - simply add the prompt='prompt text' to each input element you * want to have a prompt on. In your page header add: * jQuery(document).ready(function() { $.defaultvalue(); } * - The CSS class 'defaultvalue' is set on every element that has the defualt value * and removed when the value is no longer the default ot the user clicks on the field. * - On submit all elements with the defaultvalue class will be cleared so that server * scripts do not need additional logic to deal with presentation layer stuff. * * Sponsored by: Smartsheet.com * * * The MIT License * * Copyright (c) 2008 Canfield Research Group (robb@canfield.com) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ (function($) { $.defaultvalue = function() { var elements = jQuery('input[prompt]'); rtn = ( elements.each(function() { // Default values within scope var $el = jQuery(this); var def = $el.attr('prompt'); // Set default value and class (if blank) var blur = function() { if($el.val() == "" || $el.val() == def) { $el.addClass('defaultvalue'); $el.val(def); } }; // Remove default value and class var focus = function() { if($el.val() == def) { $el.val(""); } $el.removeClass('defaultvalue'); }; // Trigger blur event blur(); // Set functions per action $el.focus(focus); $el.blur(blur); }) ); // On all unique forms set the submit handler // - Anything with a class of defaultvalue is assumed to have the defualt value! jQuery('form').submit(function(){ jQuery(this).find('input.defaultvalue').val(''); }); return rtn; } })(jQuery)