Skip to content

Instantly share code, notes, and snippets.

@adamcrampton
Last active August 29, 2019 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrampton/3281560c3d1c3dd311ce01f3b513d0e7 to your computer and use it in GitHub Desktop.
Save adamcrampton/3281560c3d1c3dd311ce01f3b513d0e7 to your computer and use it in GitHub Desktop.
Sets a jQuery input element value in local storage
// Set field value for element if it has a value
function setFieldValueInStorage(elementId, storageKey) {
if (window.localStorage) {
let storage = window.localStorage;
// If no value, remove item. Otherwise store.
if ($('#' + elementId).val()) {
storage.setItem(storageKey, $('#' + elementId).val());
} else {
storage.removeItem(storageKey);
}
} else {
console.log('There was a problem setting local storage for element with id: ' + elementId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment