/
prefilled_input.js
59 lines (51 loc) · 1.59 KB
/
prefilled_input.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
// clear inputs with starter values
new function($) {
$.fn.prefilledInput = function() {
var focus = function () {
$(this).removeClass('prefilled');
if (this.value == this.prefilledValue) {
this.value = '';
}
};
var blur = function () {
if (this.value == '') {
$(this).addClass('prefilled').val(this.prefilledValue);
} else if (this.value != this.prefilledValue) {
$(this).removeClass('prefilled');
}
};
var extractPrefilledValue = function () {
if (this.title) {
this.prefilledValue = this.title;
this.title = '';
} else if (this.id) {
this.prefilledValue = $('label[for=' + this.id + ']').hide().text();
}
if (this.prefilledValue) {
this.prefilledValue = this.prefilledValue.replace(/\*$/, '');
}
};
var initialize = function (index) {
if (!this.prefilledValue) {
this.extractPrefilledValue = extractPrefilledValue;
this.extractPrefilledValue();
$(this).trigger('blur');
}
};
return this.filter(":input").
focus(focus).
blur(blur).
each(initialize);
};
var clearPrefilledInputs = function () {
var form = this.form || this;
$(form).find("input.prefilled, textarea.prefilled").val("");
};
var prefilledSetup = function () {
$('input.prefilled, textarea.prefilled').prefilledInput();
$('form').submit(clearPrefilledInputs);
$('input:submit, button:submit').click(clearPrefilledInputs);
};
$(document).ready(prefilledSetup);
$(document).ajaxComplete(prefilledSetup);
}(jQuery);