1

I have an accordion list from framework7 that opens and closes immediately, after investigating I realized that there are multiple click events attached to the element, how can I remove them, there should be only on click event but there are 4, is there a way to count them and if there are more than one remove the rest,

document.addEventListener("click", function (event) {
    ("event is: ", event)
}, false)

the above code gives me the MouseEvent 4 times which should only be once

MouseEvent {isTrusted: true, screenX: 42, screenY: 298, clientX: 42, clientY: 195, …}

is there a way to count the number of MouseEvents and if it's more than 1 delete the rest.

any help would be appreciated.

akano1
  • 40,596
  • 19
  • 54
  • 67
  • 1
    Are you sure the element has click events attached to it? looks like in your example the document has click events attached to it. – Francis Leigh Mar 22 '19 at 13:58
  • the code listens for all clicks on the document, and when I click on the accordion I get the event which is the MouseEvent, but it's 4 times instead of 1 so the accordion closes immediatly – akano1 Mar 22 '19 at 14:00
  • https://stackoverflow.com/questions/11455515/how-to-check-whether-dynamically-attached-event-listener-exists-or-not – Francis Leigh Mar 22 '19 at 14:00
  • You can remove event listeners https://developer.mozilla.org/es/docs/Web/API/EventTarget/removeEventListener but you need a reference to the function (move the function declaration out and put a name on it so you can remove it) – arieljuod Mar 22 '19 at 14:00
  • Putting click events on the document seems like a long way around. unless there is no way around this - attach click listeners to specific elements in the DOM, better yet save a reference to a unique event listener for each element by an ID of some sort to an Object in order to not create events if they already exist. – Francis Leigh Mar 22 '19 at 14:02
  • or set all your accordion items' .onClick = _handleClick or whatever function you are using. Having clicks on the document i'd only use to check the position of the mouse for closing Modal-Overlays for example. – Francis Leigh Mar 22 '19 at 14:04

0 Answers0