I'm currently writing a chrome extension, so don't mind the "chrome.*" code.
Right now, I'm facing a problem and I have no idea how to solve this (tried everything I could find on google)..
This is my function:
function tabwlwatchlist() {
document.getElementById('watchlisttable').innerHTML = "";
chrome.storage.local.get('movienames', function (resultnames) {
chrome.storage.local.get('movielinks', function (resultlinks) {
for (var j=0; j<resultnames.movienames.length; j++) {
(function () {
document.getElementById('watchlisttable').innerHTML += '<tr><td><h3 class="article_preview"><a href="' + resultlinks.movielinks[j] + '" target="_blank"><span>' + resultnames.movienames[j] + '</span></a></h3></td><td><h3 class="article_preview"><span id="wlt' + j +'">entfernen</span></h3></td></tr>';
console.log("test");
document.getElementById('wlt' + j).addEventListener("click", function(){
alert('works');
}, false);
console.log("test2");
}())
}
});
});
document.getElementById('lasthd').style.display = "none";
document.getElementById('lastserien').style.display = "none";
document.getElementById('watchlisttab').style.display = "block";
}
The EventListener always adds to the last Element of the for loop.
Any ideas? :/