I created a simple fidlle
var cnt = 1;
function add() {
var root = document.getElementById('root')
root.innerHTML += '<br /><a id= "a_' +cnt + '" href="#">click</a>'
var a = document.getElementById("a_"+cnt)
a.addEventListener('click', function(event) {
alert('click:a_'+cnt)
})
cnt++
}
When you click "Add" after adding a new link and after clicking on this link, a warning appears.
When more links with the Add button are added, only the last link works (others do not have a click event listener according to the devel tools).
Why does only the last link work and how can I make all links work?
source
share