Created
February 7, 2017 12:17
-
-
Save josepmartins/26ee3becb035f09c2ec4fdec79cd0512 to your computer and use it in GitHub Desktop.
Dynamic binding of new elements in a list
This file contains hidden or 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
//Dynamic binding of new elements in a list | |
// HTML code in using .pug template | |
//ul.items | |
// li Static Item | |
//button#addItem Add Item | |
const items = document.querySelector( ".items" ); | |
let counter = 0; | |
document.getElementById( "addItem" ).addEventListener( "click", () => { | |
counter++; | |
const item = document.createElement( "li" ); | |
item.textContent = `Dynamic Item ${ counter }`; | |
items.appendChild( item ); | |
} ); | |
document.querySelector( ".items" ).addEventListener( "click", e => { | |
alert( e.target.textContent ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment