Reputation: 127
I am having two bootstrap cards. On selecting one card the card details are going to the array in the local storage. Now I want is if I click another card the previous card details should be removed and the new card details should get into the array.
$("#room_next").click(function (){
localStorage.setItem("object", JSON.stringify(object));
})
var storedNames = JSON.parse(localStorage.getItem("object"));
Here #roomnext is the next button id and object is the array name as of now the two card details are storing in the array of local storage. I need only one which is selected.
Upvotes: 1
Views: 8145
Reputation: 721
If you can Identify the object that you want to delete by ex : object_Id or something. Then you can pass the object to the localStorage.removeItem('key');
in order to delete the object you want.
For example :
function populateStorage() {
localStorage.setItem('bgcolor', 'red');
localStorage.setItem('font', 'Helvetica');
localStorage.setItem('image', 'myCat.png');
localStorage.removeItem('image');
}
you can set the object like this you can remove what you want like this. In this example the image is deleted. you can refer this document to learn more.
The parameter you are passing to the removeItem() should be a String specifying the name of the item you want to remove.
Upvotes: 0
Reputation: 994
You Can Use : localStorage.removeItem('object');
to remove object saved as object
.
window.localStorage
Supports mainly 5 methods:
localStorage.setItem('key', 'value'):
/* Add key and value to localStorage */
localStorage.getItem('key') // returns 'val'
/* Retrieve a value by the key from localStorage */
localStorage.removeItem('key')
/* Remove an item by key from localStorage */
localStorage.clear()
/* Clear all localStorage */
localStorage.key(index) //retrun key
/* Passed a number to retrieve nth key of a localStorage */
Upvotes: 2
Reputation: 787
As the Mozilla Docs,
We have the method removeItem()
from the Storage
API.
You can check the API docs here;
There is another one, clear()
to remove everything.
I recommend to you to check typescript, and so you will be able to have autocomplete in your development evironment.
Upvotes: 0