I am still new to js so I can’t handle the following problem: this line creates an item in a bulleted list.
function addLi(nameGroup, classGroup) { var elem = document.getElementById("item"); var div = document.createElement("li"); div.setAttribute('class', classGroup); div.setAttribute('id', 'item'); div.innerHTML = nameGroup.toString(); elem.parentNode.appendChild(div); } But after creating an element, I cannot attach a click() event to it. When I added the same element in the html itself and then created a new one, the first one responded to clicks, and the second did not (
I have already tried different ways to add: insertBefore, appendChild
div.setAttribute('id', 'item');In this line you add an element to the element. if two identical elements add the same ID, then the second will not react, for ID must be unique for each element. - lexxl$('.sidebar__items').find(li)? - Artem Holinkadocument.querySelectorAll('.sidebar__items li')- querySelectorAll returns a list of elements within the document (the search is performed within the specified element) that correspond to the specified selector group. - lexxldiv.setAttribute('id', classGroup);but still does not respond to pressing (classGroupconstantly changing). I appeal to theitemclass. Maybe the problem is how I added a new block to the page? I js when looking for him, you simply do not see? - Artem Holinkadiv.addEventListener('click', addLi)in your method afterelem.parentNode.appendChild(div);- Vasily Barbashev