-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.placeholder.js
More file actions
113 lines (105 loc) · 3.47 KB
/
Copy pathjquery.placeholder.js
File metadata and controls
113 lines (105 loc) · 3.47 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
100
101
102
103
104
105
106
107
108
109
110
111
112
(function($) {
$.fn.placeholder = function(settings) {
var options = {
key: "placeholderValue",
attr: "placeholder",
className: "placeholder"
};
if (typeof settings == "object") {
$.extend(options, settings);
}
function focus() {
// remove the placeholder class
$(this).removeClass(options.className);
// if the value is the value of the placeholder, make it blank
if ($.trim($(this).val()) === $.data(this, options.key)) {
$(this).val("");
}
}
function blur() {
// if the value is blank, add class placeholder
// and set the value to the placeholder
if ($.trim($(this).val()) === "") {
$(this).addClass(options.className).val($.data(this, options.key));
}
}
function keyup(event) {
if (event.keyCode == 13) {
$(this).blur();
}
}
if (settings == "destroy") {
this.unbind('focus', focus);
this.unbind('blur', blur);
// NOTE: This only works if you use class placeholder.
// The problem with using the "method name in place of settings" hack
// is that you now don't have settings anymore..
this.filter('.placeholder').each(function() {
$(this).val('');
$(this).removeClass('placeholder');
});
return;
}
if (settings == "refresh") {
$(this).each(function() {
if (!$(this).attr('placeholder')) {
return;
}
if ($.trim($(this).val()) === "") {
$(this).addClass(options.className).val(
$.data(this, options.key));
}
});
return;
}
return this.filter(":input").each(function(index) {
if (!$(this).attr('placeholder')) {
return;
}
if ($(this).data('placeholderinitialized')) {
return;
}
// store the placeholder values as data
$.data(this, options.key, $(this).attr(options.attr));
var v = $.trim($(this).val());
if (v === "") {
// if the value is blank, add class placeholder
// and set the value to the placeholder
$(this).addClass(options.className).val($.data(this, options.key));
} else if (v == $.data(this, options.key)) {
// if the value is the same as the placeholder it is probably filled
// in because the user pressed the back button. So just clear it.
$(this).val('');
}
$(this).focus(focus).blur(blur).keyup(keyup).data('placeholderinitialized', true);
});
};
$.fn.serializeWithoutPlaceholders = function() {
var $this = $(this);
var dataArray = $this.serializeArray();
var $inputs;
if ($this.length == 1 && $this.find('input').length > 0) {
// this looks like a form
$inputs = $this.find('input[type=text]');
} else {
// this looks like a bunch of inputs
$inputs = $this;
}
$inputs.each(function() {
var $field = $(this);
var placeholder = $.data(this, 'placeholderValue');
if (!placeholder) {
return;
}
if ($field.val() === placeholder) {
for (var i in dataArray) {
if (dataArray[i].name == $field.attr('name')) {
dataArray[i].value = '';
break;
}
}
}
});
return $.param(dataArray);
};
})(jQuery);