I am working with an ID in HTML that is assigned to a Django variable containing ads ID. I need to add this ads ID to a favorite list using local storage (cookies). The challenge I'm facing is that the ID is inside a loop and each ad has a different ID. How can I select the specific ID when the user clicks on the corresponding icon?
Below is a snippet of my code:
{% for item in result %}
<span id="favouriteBtn" style="color:#ccc" title="add this post to favorite list"> ☆ </span>
{% endfor %}
The ID format should be as follows:
id={{item.id}}
Here is part of the JavaScript function:
$('#favouriteBtn').click(function(){
currentAddFav();
I aim to set the ID as :
id={{item.id}}
and then be able to identify the specific ID clicked by the user. How can I achieve this?
The currentAddFav function looks like this:
function currentAddFav(){
if(localStorage.getItem('favourites')){//If there are favourites
var storage = JSON.parse(localStorage['favourites']);
if (storage.indexOf('data-item-id') == -1) {
// # not found
storage.push('data-item-id');
localStorage.setItem('favourites', JSON.stringify(storage));
} else {
// # found
console.log('item already in favorites')
}
}
else
{//No favourites in local storage, so add new
var favArray= [];
favArray.push('data-item-id');
localStorage.setItem("favourites", JSON.stringify(favArray));
console.log('New favorites list');
}
}