-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinputStore.jquery.js
More file actions
74 lines (61 loc) · 2.14 KB
/
inputStore.jquery.js
File metadata and controls
74 lines (61 loc) · 2.14 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
(function ( $ ) {
function inputStoreSet(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = d.toUTCString();
document.cookie = cname + "=" + cvalue + "; expires=" + expires + "; path=/";
};
function inputStoreGet(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
};
function store(settings, type, dom) {
if(type === "checkbox") {
inputStoreSet(settings.name, $(dom).prop('checked'), settings.expire);
if(settings.debug) {
console.log("Previous Input for ["+settings.name+"] = ["+$(dom).prop('checked')+"]");
}
} else {
inputStoreSet(settings.name, dom.value, settings.expire);
if(settings.debug) {
console.log("Previous Input for ["+settings.name+"] = ["+dom.value+"]");
}
}
}
$.fn.inputStore = function( options ) {
// This is the easiest way to have default options.
var settings = $.extend({
name: this.attr("name"),
expire: 365,
debug: false
}, options );
var previousSet = inputStoreGet(settings.name);
var type = this.attr("type") || this[0].nodeName;
if(type && type == "checkbox" && previousSet == "true"){
this.prop('checked', previousSet);
} else {
this.val(previousSet);
}
if(settings.debug) {
console.log("Previous Input for ["+settings.name+"] = ["+previousSet+"]");
}
this.on('keyup', function(){
store(settings, type, this);
});
this.on('change', function(){
store(settings, type, this);
});
return this;
};
}( jQuery ));