Scenario
- The user clicks on the item, the background image of this item has been changed, and the data assigned to this item (attribute
onclick) is inserted into localStorage: this part works fine. Now the variable is toggleset to 0, the background image is changed and the data is deleted from the local storage: this part works fine.
...
The user clicks on another element (first click data is deleted from the previous div). although in this case you need to insert new data, but is it not?
Json data:
Object {
sess_id : 182104,
name : "AUTOMECH FORMULA",
city : "Cairo",
country : "Egypt",
event_url : "automech-formula"
}
events:189 Object {
sess_id : 182104,
name : "AUTOMECH FORMULA",
city : "Cairo",
country : "Egypt",
event_url : "automech-formula"
}
snapshot (for all data deleted by a single clicked item): -

HTML:
<div class="evt_date" style="overflow:hidden" style="overflow:hidden" itemscope itemtype="http://schema.org/Event">
<a href="javascript:void(0);" class="favourate_dextop" id="fav'.$data[$k]['id'].'" onClick=" favaorite('.$data[$k]['id'].',\''.$name_event.'\',\''.$event_city.'\',\''.$event_country.'\',\''.$event_urls.'\',this)"></a>
</div>
JavaScript:
var image2 = 'http://im.gifbt.com/images/star1_phonehover.png';
var image1 = 'http://im.gifbt.com/images/star1_phone.png';
var toggle = 1;
function favaorite(sess_id,name,city,country,event_url,pointer){
var eventData;
if (localStorage.getItem('eventData') === null) {
eventData = [];
}else{
eventData = JSON.parse(localStorage.getItem('eventData'));
console.log(eventData);
}
var details={};
details.sess_id = sess_id;
details.name = name;
details.city = city;
details.country = country;
details.event_url = event_url;
eventData.push(details);
if (toggle == 1){
console.log("1");
$(pointer).closest('.evt_date').find('.favourate_dextop').css('background-image', 'url("' + image2 + '")');
toggle = 0;
}else{
console.log("2");
$(pointer).closest('.evt_date').find('.favourate_dextop').css('background-image', 'url("' + image1 + '")');
$.each(eventData, function(key, value){
console.log(value);
delete value.sess_id;
delete value.name;
delete value.city;
delete value.country;
delete value.event_url;
});
toggle = 1;
}
var jsondata=localStorage.setItem('eventData', JSON.stringify(eventData));
console.log(jsondata);
}
Fiddle