Last active
August 29, 2019 02:27
-
-
Save adamcrampton/3281560c3d1c3dd311ce01f3b513d0e7 to your computer and use it in GitHub Desktop.
Sets a jQuery input element value in local storage
This file contains hidden or 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
// 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