Skip to content

Instantly share code, notes, and snippets.

@josepmartins
Created February 7, 2017 12:17
Show Gist options
  • Save josepmartins/26ee3becb035f09c2ec4fdec79cd0512 to your computer and use it in GitHub Desktop.
Save josepmartins/26ee3becb035f09c2ec4fdec79cd0512 to your computer and use it in GitHub Desktop.
Dynamic binding of new elements in a list
//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