This repository was archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.defaultfieldtext.js
More file actions
65 lines (53 loc) · 1.4 KB
/
jquery.defaultfieldtext.js
File metadata and controls
65 lines (53 loc) · 1.4 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
/*!
* Default Field Text jQuery Plugin
* Copyright (c) 2009 Johan Sahlén
* Licensed under the MIT license.
*/
(function($){
$.fn.defaultFieldText = function(options) {
options = jQuery.extend({
defaultClass: "default",
text: null,
textSuffix: "",
resetOnSubmit: true
}, options);
var setText = function(clear) {
var field = $(this);
var cur = field.val();
var def = field.data('defaultText');
if (clear && cur == def) {
field.removeClass(options.defaultClass);
field.val("");
}
if (!clear && (cur.match(/^\s*$/) || cur == def)) {
field.addClass(options.defaultClass);
field.val(def);
}
};
this.each(function() {
var field = $(this);
var text = "";
if (options.text) {
text = options.text;
} else {
var id = field.attr("id");
var label = $("label[for="+id+"]");
text = label.text();
}
field.data("defaultText", text+options.textSuffix);
field.bind("focus", function() {
setText.call(this, true);
}).blur(function() {
setText.call(this);
});
setText.call(field);
if (options.resetOnSubmit) {
var form = field.parents("form:first");
form.bind("submit", function() {
setText.call(field, true);
});
}
});
return this;
};
}(jQuery));