Scenario
- A user clicks on an element, image background of that element has been changed and the data assigned to that element (
onclick
attribute) is inserted into localStorage: this part works fine. Now
toggle
variable is set to0
, background image is changed and the data is being deleted from local storage : this part works fine....
A user clicks on another element (first click data is deleted of prev div ). though a new data should be inserted in that case, but it does 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 removed on particular div clicked):-
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;
//is anything in localstorage?
if (localStorage.getItem('eventData') === null) {
eventData = [];
}else{
// Parse the serialized data back into an array of objects
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;
// Push the new data (whether it be an object or anything else) onto the array
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);
//$('#fav'+ delete value.sess_id + '');
//$('#fav'+ delete value.name + '');
//$('#fav'+ delete value.city + '');
//$('#fav'+ delete value.country + '');
//$('#fav'+ delete value.event_url + '');
delete value.sess_id;
delete value.name;
delete value.city;
delete value.country;
delete value.event_url;
//localStorage.removeItem(value.sess_id);
});
toggle = 1;
}
// Alert the array value
// alert(eventData); // Should be something like [Object array]
// Re-serialize the array back into a string and store it in localStorage
var jsondata=localStorage.setItem('eventData', JSON.stringify(eventData));
console.log(jsondata);
}