Event listener for dynamically created html <p>I am dynamically generating html, and I want the elements I add to have event listeners. The "board" div is in my html file, and I create the "myDiv" element. This part works.</p> <pre><code>var board = document.getElementById("board"); var myDiv = document.createElement('div'); myDiv.innerHTML="this is my div"; </code></pre> <p>Then I try to add an event listener to the "myDiv" div. I have tried all of the following:</p> <pre><code>myDiv.onclick=function(){alert('click')}; myDiv.addEventListener('click', function(){alert('click')}); myDiv.onclick=myFunction //myFunction just creates an alert like the others </code></pre> <p>Then I append the 'myDiv' element to the board. It shows up on the screen as expected</p> <pre><code>board.appendChild(myDiv); </code></pre> <p>The div elements show up as expected, and when I open the javascript console in chrome it looks like that part is correct. Also, the javascript console says that there is a 'click' event listener attached to the 'myDiv' div, but no alert ever comes up like it should. How can I fix this? Thanks!</p>