Skip to content

Instantly share code, notes, and snippets.

Last active Jan 27, 2021
What would you like to do?
<!DOCTYPE html>
<title>Event Listener - JavaScript</title>
<div class="static">Static</div>
<button class="add" onclick="return false;">Add</button>
<script type="text/javascript">
document.querySelector( '.add' ).addEventListener( 'click', addElement );
function addElement() {
var static = document.querySelector( '.static' );
var dynamic = document.createElement( 'div' );
dynamic.classList.add( 'dynamic' );
dynamic.innerText = 'Dynamic';
static.insertAdjacentElement( 'afterend', dynamic );
setListener( dynamic );
function setListener( el ) {
// Dynamic elements event listener
el.addEventListener( 'click', function() {
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment