Javascript - Addeventlistener On All Created Li Elements May 23, 2023 Post a Comment So I have a simple script that adds 'li' elements to the 'ul' and assigns them a class. Now I want to change the class of 'li' item on click event. Here is the HTML: Solution 1: Use event delegation for the dynamically created elements. With this, you only need one event listener on the ul#list and it will work for all elements you dynamically attach to it: document.getElementById("list").addEventListener("click",function(e) { if (e.target && e.target.matches("li.item")) { e.target.className = "foo"; // new class name here } }); Copy Here's a simplified example so you can see what happens with the code: function addItem(i) { var li = document.createElement('li'); li.appendChild(document.createTextNode(i)); li.className = 'item'; document.getElementById('list').appendChild(li); } var counter = 2; document.getElementById('btn').addEventListener('click', function() { addItem(counter++); }); document.getElementById("list").addEventListener("click", function(e) { if (e.target && e.target.matches("li.item")) { e.target.className = "foo"; // new class name here alert("clicked " + e.target.innerText); } });Copy <ul id="list"> <li class="item">1</li> </ul> <button id="btn"> add item </button>Copy Solution 2: You'll have to set the eventListener on each single item, as document.getElementsByClassName() returns a collection of items and you can't simply add an event listener to all of them with one call of addEventListener(). So, just like the loop you used in itemDone(), you'll have to iterate over all items and add the listener to them: var items = document.getElementsByClassName("item"); for (var i = 0; i < items.length; i++) { items[i].addEventListener("click", itemDone); } Copy As pointed out in the comments, you can also do so directly when creating the elements, so in your addItem() function, add:Baca JugaSelect2 Doesn't Render ProperlyApply A Oil Paint/sketch Effect To A Photo Using JavascriptBootstrap 3 Collapse On Hover newListItem.addEventListener("click", itemDone); Copy Solution 3: try this : var item = document.getElementsByClassName("item"); for (var i = 0; i < item.length; i++) { item[i].addEventListener("click", itemDone); } Copy Solution 4: function addItem(i) { var li = document.createElement('li'); li.appendChild(document.createTextNode(i)); li.className = 'item'; document.getElementById('list').appendChild(li); } var counter = 2; document.getElementById('btn').addEventListener('click', function() { addItem(counter++); }); document.getElementById("list").addEventListener("click", function(e) { if (e.target && e.target.matches("li.item")) { e.target.className = "foo"; // new class name here alert("clicked " + e.target.innerText); } });Copy <ul id="list"> <li class="item">1</li> <li class="item">1</li> <li class="item">1</li> <li class="item">1</li> <li class="item">1</li> </ul> <button id="btn"> add item </button>Copy Solution 5: First, try using getElementByTagName instead of querySelectorAll, because querySelectorAll is slower. And second, item receives an array, so item.addEventListener will give you an error. You have to do the addEventListener over item[counter], in a loop. Share You may like these postsEnforcing The Maxlength Attribute On Mobile BrowsersHow To Draw Ecg Monitor In Canvas Using Html5?Url Fragments And The Base TagHow To Have Multiple Tabbed Sections Open The First Tab On Load Using Html, Css, And Javascript? Post a Comment for "Javascript - Addeventlistener On All Created Li Elements"
Post a Comment for "Javascript - Addeventlistener On All Created Li Elements"