-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsavestorage.js
73 lines (58 loc) · 2.45 KB
/
savestorage.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
/**
* jQuery saveStorage - 23.06.2019
* Version: 2.0.0
* Website: https://github.com/sarkhanrajabov/saveStorage
* Author: Sarkhan Rajabov
**/
(function($){
$.fn.saveStorage = function(options){
'use strict';
if(typeof Storage !== "undefined"){
var form = $(this),
key = $(this).attr('id')+'_saveStorage',
defaults = {
exclude: []
};
var opts = $.extend({}, defaults, options);
var excludeInputType = function(){
var inputType = '';
$.each(opts.exclude, function(k,v){
inputType += 'input[type='+v+'],'
});
return inputType;
};
form.find(':input').bind('change keyup', function () {
var serializeForm = form.serializeArray();
localStorage.setItem(key, JSON.stringify(serializeForm));
});
var initApp = function(){
if(localStorage.getItem(key) !== null){
var data = JSON.parse(localStorage.getItem(key)),
inputRadio = form.find('input[type=radio]'),
inputCheckbox = form.find('input[type=checkbox]');
for(var i = 0; i < data.length; i++){
form.find(':input[name='+data[i].name+']')
.not(excludeInputType() + 'input[type=radio], input[type=checkbox]').val(data[i].value);
for(var j = 0; j < inputRadio.length; j++){
if(inputRadio[j].getAttribute('name') === data[i].name && inputRadio[j].getAttribute('value') === data[i].value){
inputRadio[j].checked = true;
}
}
for(var k = 0; k < inputCheckbox.length; k++){
if(inputCheckbox[k].getAttribute('name') === data[i].name && inputCheckbox[k].getAttribute('value') === data[i].value){
inputCheckbox[k].checked = true;
}
}
}
}
};
form.submit(function () {
localStorage.removeItem(key);
});
initApp();
}
else {
console.error('Sorry! No web storage support.')
}
};
})(jQuery);