-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblk.jquery.placeholder.js
76 lines (62 loc) · 1.58 KB
/
blk.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
/*!
* jQuery placeholder polyfill
*
* @author: Brandon Lee Kitajchuk
* http://blkcreative.com
*
* Tested in IE6-9, Firefox, Safari, Chrome and Opera
*
* callback: function () {},
* polyClass: "placeholder",
* wrapClass: "placeholder-wrap",
* tagName: "span"
*
*/
(function ( $ ) {
var inp = document.createElement( "input" );
$.support.placeholder = "placeholder" in inp;
inp = null;
$.fn.extend({
placeholder: function () {
if ( $.support.placeholder ) {
return false;
}
var settings = {
callback: function () {},
polyClass: "placeholder",
wrapClass: "placeholder-wrap",
tagName: "span"
};
if ( typeof arguments[0] === "function" ) {
settings.callback = arguments[0];
} else {
$.extend( settings, arguments[0] );
}
return this.filter( "[placeholder]" ).each(function ( i ) {
var $input = $( this ),
$placeholder = $( "<" + settings.tagName + " />", {
"class": settings.polyClass,
id: "ph-" + ( i + 1 ),
text: $input.attr( "placeholder" )
}),
$wrapper = $( "<" + settings.tagName + " />", {
"class": settings.wrapClass
});
$input.wrap( $wrapper ).focus(function ( event ) {
$placeholder.text( "" );
settings.callback.apply( this, arguments );
}).blur(function () {
if ( this.value !== "" ) {
$placeholder.text( "" );
} else {
$placeholder.text( $( this ).attr( "placeholder" ) );
}
});
$placeholder.insertAfter( $input ).click(function ( event ) {
$placeholder.text( "" );
$input.focus();
});
});
}
});
})( jQuery );