Unsetting a value from local storage on click
I'm using local storage to save the id as seen below, it works as it's
suppose to. When I click on the anchor it sends the value to local
storage. The problem is when I click it again, it saves that same value
again. I would like to take that value out of storage on the second click.
So basically I want it to act as a toggle.
$(".favorites").click(function() {
var favorite = JSON.parse(localStorage.getItem( 'favorite' ));
if (favorite == undefined)
{
favorite = Array();
}
favorite.push($(this).attr('data-petid'));
localStorage.setItem( 'favorite', JSON.stringify(favorite) );
JSON.parse( localStorage.getItem( 'favorite' ) );
});
No comments:
Post a Comment