JavaScript - AddEventListener On All Created Li Elements September 02, 2022 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 JugaContent Hiding Above Div'sCalculate Height With Jquery For Sticky HeaderIssue In Width Of The Chart 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 postsJavascript Audio Plays TwiceHow To Set Src Of Image By A Function Call?How To Capture Content Typed In A Rich Text Editor Div - Quill Rich EditorMerge Equal Table Cells With Jquery Post a Comment for "JavaScript - AddEventListener On All Created Li Elements"
Post a Comment for "JavaScript - AddEventListener On All Created Li Elements"