Forked from martindrapeau/jquery.cach-inputs.js
Last active
November 30, 2019 16:16
-
-
Save lklepner/53f14fbfd6347813f0b9 to your computer and use it in GitHub Desktop.
jQuery plugin to cache HTML inputs using local storage. Restores them upon next page load.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cache user inputs into HTML5 local storage. Restore them upon next page load. | |
// Usage: | |
// $('body').CacheInputs(); | |
// | |
// Will store stringified JSON in local storage, against the key you specify | |
// or 'cache-inputs' if you omit it. See settings below. | |
// | |
// Author: Martin Drapeau | |
// | |
// Update 12/14/2015 - Added execution of options param to function as a callback when values have finished populating | |
// | |
(function($) { | |
$.fn.CacheInputs = function(options) { | |
var settings = $.extend({ | |
key: 'cache-inputs', | |
inputs: [ | |
'input[type=text]', | |
'input[type=radio]', | |
'input[type=checkbox]', | |
'select' | |
] | |
}, options); | |
function on_change(event) { | |
var $input = $(event.target), | |
key = settings.key, | |
id = $input.attr('id'), | |
data = JSON.parse(localStorage[key]); | |
if ($input.attr('type') == 'radio' || $input.attr('type') == 'checkbox') { | |
data[id] = $input.is(':checked'); | |
} else { | |
data[id] = $input.val(); | |
} | |
localStorage[key] = JSON.stringify(data); | |
} | |
return this.each(function() { | |
var element = $(this), | |
selector = settings.inputs.join(', '); | |
if (typeof(Storage) !== "undefined") { | |
var key = settings.key; | |
var data = false; | |
if (localStorage[key]) { | |
data = JSON.parse(localStorage[key]); | |
} | |
if (!data) { | |
localStorage[key] = JSON.stringify({}); | |
data = JSON.parse(localStorage[key]); | |
} | |
element.find(selector).change(on_change); | |
element.find(selector).each(function() { | |
if ($(this).attr('type') != 'submit') { | |
var $input = $(this), | |
id = $input.attr('id'), | |
value = data[id]; | |
if (value === undefined) return true; | |
if ($input.attr('type') == 'radio' || $input.attr('type') == 'checkbox') { | |
if (value) { | |
$input.attr('checked', true); | |
} else { | |
$input.removeAttr('checked'); | |
} | |
} else { | |
$input.val(value); | |
} | |
} | |
}); | |
options(); | |
} | |
}); | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment