5 focusClass: 'placeholderFocus',
6 activeClass: 'placeholder',
7 overrideSupport: false,
8 preventRefreshIssues: true
12 if(!$.placeholder.debug) return;
13 msg = "[Placeholder] " + msg;
14 $.placeholder.hasFirebug ?
16 $.placeholder.hasConsoleLog ?
17 window.console.log(msg) :
20 hasFirebug : "console" in window && "firebug" in window.console,
21 hasConsoleLog: "console" in window && "log" in window.console
26 // check browser support for placeholder
27 $.support.placeholder = 'placeholder' in document.createElement('input');
29 // Replace the val function to never return placeholders
30 $.fn.plVal = $.fn.val;
31 $.fn.val = function(value) {
32 $.placeholder.log('in val');
34 $.placeholder.log('have found an element');
36 if(value != undefined)
38 $.placeholder.log('in setter');
39 var currentValue = el.plVal();
40 var returnValue = $(this).plVal(value);
41 if(el.hasClass($.placeholder.settings.activeClass) && currentValue == el.attr('placeholder')){
42 el.removeClass($.placeholder.settings.activeClass);
47 if(el.hasClass($.placeholder.settings.activeClass) && el.plVal() == el.attr('placeholder')) {
48 $.placeholder.log('returning empty because its a placeholder');
51 $.placeholder.log('returning original val');
55 $.placeholder.log('returning undefined');
59 // Clear placeholder values upon page reload
60 $(window).bind('beforeunload.placeholder', function() {
61 var els = $('input.placeholderActive' );
63 els.val('').attr('autocomplete','off');
68 $.fn.placeholder = function(opts) {
69 opts = $.extend({},$.placeholder.settings, opts);
71 // we don't have to do anything if the browser supports placeholder
72 if(!opts.overrideSupport && $.support.placeholder)
75 return this.each(function() {
78 // skip if we do not have the placeholder attribute
79 if(!$el.is('[placeholder]'))
82 // we cannot do password fields, but supported browsers can
83 if($el.is(':password'))
86 // Prevent values from being reapplied on refresh
87 if(opts.preventRefreshIssues)
88 $el.attr('autocomplete','off');
90 $el.bind('focus.placeholder', function(){
92 if(this.value == $el.attr('placeholder') && $el.hasClass(opts.activeClass))
94 .removeClass(opts.activeClass)
95 .addClass(opts.focusClass);
97 $el.bind('blur.placeholder', function(){
100 $el.removeClass(opts.focusClass);
103 $el.val($el.attr('placeholder'))
104 .addClass(opts.activeClass);
107 $el.triggerHandler('blur');
109 // Prevent incorrect form values being posted
110 $el.parents('form').submit(function(){
111 $el.triggerHandler('focus.placeholder');