-
Notifications
You must be signed in to change notification settings - Fork 2
/
jqplaceholder.js
executable file
·61 lines (53 loc) · 1.69 KB
/
jqplaceholder.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
/**
* jQPlaceholder - https://github.com/pedrochaves/jQPlaceholder
*
* A jQuery plugin to simulate the HTML5 placeholder in browsers
* that don't support it
*
* @author Pedro Chaves (https://github.com/pedrochaves/)
*/
(function(window, $){
/**
* Initiates the plugin
*/
var _init = function (self) {
var type = self.attr("type"),
placeholder = self.attr("placeholder") || self.attr("value");
if ($.inArray(type, ["password", "submit", "button"]) > -1) {
return false;
}
// if the browser supports HTML5 placeholder and the attribute
// placeholder exists, do nothing
if("placeholder" in window.document.createElement("input")){
if (self.attr("placeholder")) {
return false;
}
}
// Setting up initial value
if(!self.attr("value") && type === "text"){
self.attr("value", placeholder);
}
self.live({
"focus.jqplaceholder": function () {
if(self.attr("value") === placeholder){
self.attr("value", "");
}
},
"blur.jqplaceholder": function () {
if(!self.attr("value")){
self.attr("value", placeholder);
}
}
});
};
// General, standalone way to use it
$.jQPlaceholder = function () {
$("input[placeholder], input[value]").jQPlaceholder();
};
// Here is the plugin :)
$.fn.jQPlaceholder = function () {
return this.each(function () {
_init($(this));
});
};
}(window, jQuery));