Posts

Showing posts with the label addeventlistener

Why doesn't triggering click() inside a click event listener cause an infinite loop?

50 8 Can someone explain the program flow of this JavaScript code: const $leaveRoom = document.querySelector('#leave-button'); let a = 1; $leaveRoom.addEventListener('click', () => { console.log(a); console.log("check"); a++; $leaveRoom.click(); console.log(a); a++; }); <button id="leave-button">Leave Room</button> The Output was: 1 check 2 check 3 4 This question may sound dumb but I am new to JavaScript. I am not able to understand the program flow of this code. I want to know how did I get 3 & 4 in the output. javascript html addeventlistener ...

How to make addEventListener 'click' work on two menu items with same class

7 1 Page: https://ensjotannklinikk.no/forside-wip Here's a fiddle The first menu item called " Behandlinger " is set up with this JS (courtesy of biberman) to make the submenu emerge and retract properly. var submenu = document.querySelector('.behandlinger-meny'); var menuTrigger = document.querySelector('.behandlinger-item'); //javascript version of jQuery isChild() function isChild(item, parentItem) { while (item != undefined && item != null && item.tagName.toUpperCase() != 'BODY'){ if (item == parentItem){ return true; } item = item.parentNode; } return false; } menuTrigger.addEventListener('click', function() { submenu.style.height = '55px';...