-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.placeholder.js
More file actions
99 lines (86 loc) · 2.34 KB
/
jquery.placeholder.js
File metadata and controls
99 lines (86 loc) · 2.34 KB
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
/**
* Placeholder - v1
*
* Terms of Use // Placeholder
*
* Copyright (c) 2012, White Whale Web Services
* All rights reserved.
*/
(function($) {
"use strict";
var defaults, methods;
// default options
defaults = {
className: "placeholder",
clearSelector: false,
onlyIfNeeded: true,
placeholderValue: ""
};
methods = {
// init
// start up placeholder
init: function(options) {
var settings;
options = (typeof options === "object") ? options : {};
settings = $.extend({}, defaults, options);
return this
.each(function() {
var self = $(this);
// if browser supports placeholders for this, abort
if (settings.onlyIfNeeded && this.placeholder && "placeholder" in document.createElement(this.tagName)) {
return;
}
// abort to avoid double-binding
if (self.data("placeholder")) {
return;
}
// set the placeholder value, overriding any passed in option
settings.placeholderValue = self.attr("placeholder") || "";
self
.data("placeholder", settings) // set settings to prevent double-binding
.focus(function() {
methods.clear.apply(self);
})
.blur(function() {
if (!self.val() || self.val() == settings.placeholderValue) {
self
.addClass(settings.className)
.val(settings.placeholderValue);
}
})
.blur(); // trigger blur now
// clear on submit and on click of configured elements
self
.parents("form")
.submit(function() {
methods.clear.apply(self);
});
$(settings.clearSelector)
.live("click", function() {
methods.clear.apply(self);
});
})
},
// clear
// clears the placeholder
clear: function() {
var self = $(this),
settings = self.data("placeholder");
if (self.val() == settings.placeholderValue) {
self
.removeClass(settings.className)
.val("");
}
}
}
// activate the plugin
$.fn.placeholder = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || $.isFunction(method) || !method) {
return methods.init.apply(this, arguments);
} else {
$.error("Method " + method + " does not exist for jQuery.placeholder.");
}
}
})(jQuery);