-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.inputvalue.js
145 lines (110 loc) · 4.14 KB
/
jquery.inputvalue.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
inputValue 1.0.1 | 14/Oct/2013
Developed by: V. Radev - http://v.radev.info
Copyright (C) 2013 V. Radev
*/
(function($){
$.fn.inputValue = function(options){
options = $.extend({
value: 'Default Value',
valueSource: 'direct',
removeTitle: false,
reAppear: true,
dataKey: 'value',
textOpacity: '',
formHandle: '',
alertSubmit: '',
alerted: false
}, options);
var elements = 0;//count objects
return this.each(function(){
var o = options,
obj = $(this),
defValue;
// GETTING FIELD DEF VALUE [[
switch (o.valueSource) {
case 'direct':
if ( $.isArray(o.value) ){//If array of values is passed
var multi = true;
} else {//if single value
defValue = o.value;
}
break;
case 'title':
defValue = obj.attr('title');
break;
case 'data':
defValue = obj.data(o.dataKey);
break;
default:
alert('Invalid valueSource!');
}
// GETTING FIELD DEF VALUE ]]
if ( o.removeTitle ){
obj.removeAttr('title');
}
if ( multi ){//If array of values is passed
obj.val(o.value[elements]);
if (o.textOpacity ){
obj.css('color', 'rgba(0, 0, 0, '+ o.textOpacity +')');
}
obj.on("focus", function(){
$(this).val('');
if (o.textOpacity ){
$(this).css('color', 'rgba(0, 0, 0, 1)');
}
});//end focus
if (o.formHandle ){
$(o.formHandle).on("submit", function(e){
for(var i = 0; i < o.value.length; i++){
if ( obj.val() === o.value[i] ){
e.preventDefault();
var submitted = true;
}
}//end for loop
if ( submitted ){
if (o.alertSubmit && !o.alerted ){
alert(o.alertSubmit);
o.alerted = true;
}
}//if submitted and aler is On
});
}//if submit with def values
elements++;
} else {//if single value
obj.val(defValue);
if (o.textOpacity ){
obj.css('color', 'rgba(0, 0, 0, '+ o.textOpacity +')');
}
obj.on("focus", function(){
if (o.textOpacity ){
$(this).css('color', 'rgba(0, 0, 0, 1)');
}
if ( $(this).val() === defValue ){
$(this).val('');
}
});//end focus
if ( o.reAppear ){
obj.on("blur", function(){
if ( $(this).val() === '' ){
$(this).val(defValue);
if (o.textOpacity ){
obj.css('color', 'rgba(0, 0, 0, '+ o.textOpacity +')');
}
}
});//end blur
} //end if reApper
if (o.formHandle ){
$(o.formHandle).on("submit", function(e){
if ( obj.val() === defValue ){
e.preventDefault();
if (o.alertSubmit ){
alert(o.alertSubmit);
}
}
});
}//if submit with def values
}// end multi
});//end each
}
})(jQuery);