I am using innerHTML to add some elements and wrapper on top of it
for (i = 0; i < arr.length; i++) {
b.innerHTML += "<div class='wrapper'><div class='col-4'>" + arr[i].storeID + "</div>" +
"<div class='col-4'>" + arr[i].Bookid + "</div>" +
"<div class='col-4'>" + arr[i].StoreName + "</div>" + "</div>"
var d=document.querySelector('.wrapper')
d.addEventListener("click", function(e) {
console.log('on click fired')
/*insert the value for the autocomplete text field:*/
inpStr=this.innerHTML
inp.value = this.innerHTML
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
}
The innerHTML is working as expected but when I click the wrapper row, I get
Cannot Cannot read property 'addEventListener' of null when using innerHTML
Is this because I am using innerHTML to create an element?
.wrapperelement is still in the document? Can you create a minimal reproducible example that we can debug? Keep in mind that assigning to theinnerHTMLof an element will destroy all listeners on any children of that elementbthe problem is related to what is b here ??bis just a DOM element that I made a few lines above the code.b = document.createElement("DIV");d.addEventListenerthan I can see that the.wrapperelement is created properly.