0

how can I add event listeners to elements added in html with javascript like this ?

items.forEach((cors) => {
    const { top, left, width, height } = cors
    container.innerHTML +=
      `<div class='box' 
      style='top:${top}px;left:${left}px;width:${width}px;height:${height}px'>`

  })

I tried to make it with javascript but it did not work

  • Use [event delegation](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation) instead of adding several event listeners — it’s more maintainable and applies to dynamically added elements. See [the tag info](/tags/event-delegation/info) and [this Q&A](/a/34896387/4642212). – Sebastian Simon Dec 26 '22 at 09:09

0 Answers0