-
-
Save maurisrx/28cc1b18147ef1b5b46ea84ff4f78abf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Event Listener - JavaScript</title> | |
</head> | |
<body> | |
<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 ); | |
} | |
var dynamicElements = document.querySelectorAll( '.dynamic' ); | |
for ( var i = 0; i < dynamicElements.length; i++ ) { | |
dynamicElements[i].addEventListener( 'click', function() { | |
this.remove(); | |
} ); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment