-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.placeholder.js
executable file
·111 lines (85 loc) · 2.46 KB
/
jquery.placeholder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
(function(window, $, undefined) {
"use strict"; // jshint
/* **
* placeholder:
* fallback for browsers that don't support the 'placeholder' attribute
*
* browser support: http://caniuse.com/#search=placeholder (e.g. no IE7-9)
*
***
* options:
*
* 'complete'
* callback function
*/
$.fn.placeholder = function( options ) {
var opts = $.extend( {}, $.fn.placeholder.defaults, options ),
$elems;
if ( !( 'placeholder' in document.createElement('input'))) {
$elems = $(this).find( '[placeholder]' );
$elems
// remove placeholder value on focus
.on( 'focus', function() {
var $input = $(this),
placeHolderText = $input.attr('placeholder');
if ( $input.val() === placeHolderText ) {
$input
.val('')
.removeClass('placeholder');
}
})
// add placeholder text on blur if no individual value is set
.on( 'blur', function() {
var $input = $(this),
placeHolderText = $input.attr('placeholder');
if ( $input.val() === '' ) {
$input
.addClass('placeholder')
.val(placeHolderText);
}
})
// initiate placeholder text
.blur();
// form submit
$elems
.parents('form')
.find(opts.submitHandler)
.on( 'click', function( evt ) {
evt.preventDefault();
var $this = $(this),
$placeholderElems = $this.parents('form').find('[placeholder]');
// remove placeholder text
$placeholderElems
.each( function() {
var input = $(this),
placeHolderText = input.attr('placeholder');
if ( input.val() === placeHolderText ) {
input.val('');
}
});
// add placeholder text back
setTimeout( function() {
$placeholderElems
.each( function() {
var input = $(this),
placeHolderText = input.attr('placeholder');
if ( input.val() === '' || input.val() === placeHolderText ) {
input
.val(placeHolderText)
.addClass('placeholder');
}
});
}, 10);
});
}
opts.complete.call( this );
// ensure method calls can be chained
// http://www.sitepoint.com/10-tips-better-jquery-plugins/
return this;
};
// Plugin defaults – added as a property on the plugin function.
$.fn.placeholder.defaults = {
submitHandler: '.js-form-submit',
complete: function() {}
};
}( window, window.jQuery ));