So basically I have to following script. When I click it, data gets pushed into an array. But whenever I click it, the array size just stays the same and elements aren’t being added, it will just stay one element long. What am I missing?
$(document).on('click', '#favoriteadd', function() {
var favorites = [];
favorites.push($('h5').text());
console.log(favorites);
});
You’re initializing the
favoritesvariable to empty each time you click.Try this: