javascript - How to add a class to parent element using on click event listener? -
i'm trying add is-selected
class each widget user clicks on. js , widgets have following structure:
const widgets = document.getelementsbyclassname('widget'); array.from(widgets, wgt => wgt.addeventlistener('click', function(event) { // handlers here event.target.classlist.add('is-selected'); // more handlers here }));
<div class="my-list"> <div class="widget" id="widget-#"> <div> <h1>title</h1> <h2>unique #</h2> </div> </div> </div>
but whenever click on widget, is-selected
added onto 1 of children (supposedly 1 clicked). doing wrong? want is-selected
added widget , not of child elements.
Comments
Post a Comment