i want the value from newUsername
to be added to the localStorage
setUsernamesArray, whereas in my code, it replaces the entire value.
$('#signUpButton').click(function() {
var newUsername = $('#usernameSignInBox').val();
signedUpUsernames.push(newUsername);
localStorage.setItem('setUsernamesArray', signedUpUsernames);
});
If you want to add to the array, you must realise that the value in localStorage isn't an array, it is a string. If you want that string to represent an array, and you want to update that array, you must do the following things:
localStorage.getItem('setUsernamesArray')
, JSON.parse
, localStorage.setItem('setUsernamesArray', JSON.stringify(array))
localStorage
always store values as a string
, so stringify
your array
and store,
localStorage.setItem('setUsernamesArray', JSON.stringify(signedUpUsernames));
And before reading it just parse
it,
var arrayFrmStorage = JSON.parse(localStorage.getItem("setUsernamesArray"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With