function
findAll()
Option name | Type | Description |
---|---|---|
el | Element | |
cb | Function |
Find all the form elements inside a given element and run a callback on each.
function findAll(el, cb) {
var inputs = el.querySelectorAll('input, select, textarea');
var i = 0;
var len = inputs.length;
for (; i < len; i++) {
cb(inputs[i]);
}
}
return {
method
store()
Option name | Type | Description |
---|---|---|
el | Element |
Find all the form elements inside a given element and store their current
value as a data attribute.
store: function(el) {
findAll(el, function(input) {
var name = input.nodeName.toLowerCase();
var value;
switch (name) {
case 'select':
value = input.selectedIndex;
break;
default:
value = encodeURI(input.value);
break;
}
input.setAttribute('data-stored-value', value);
});
},
method
restore()
Option name | Type | Description |
---|---|---|
el | Element |
Revert all the form elements inside of a given element.
restore: function(el) {
findAll(el, function(input) {
var name = input.nodeName.toLowerCase();
var value = input.getAttribute('data-stored-value');
// No stored value
if (!value && value !== '') {
return;
}
switch (name) {
case 'select':
input.options[value].selected = true;
break;
default:
input.value = decodeURI(value);
break;
}
input.removeAttribute('data-stored-value');
});
},
method
clear()
Option name | Type | Description |
---|---|---|
el | Element |
Clear the stored data on all the form elements inside of a given element.
clear: function(el) {
findAll(el, function(input) {
input.removeAttribute('data-stored-value');
});
}
};
}));