Skip to content
Advertisement

Javascript event listener problems

Supposing we have :

JavaScript

When the calendar is updated, the event will not be replaced, so after a click event in the calendar, it will update one time but will not re-put the event listener provided by awesomeFunction.

So what’s the problems ?

Advertisement

Answer

You can use event delegation:

JavaScript

When you attach an event listener to an element, the listener exist as long as this element exists in the DOM. So, when you update the calendar, these elements are deleted, so the event listener attached to them too.

With event delegation, you are setting a delegate (a static element like document) that is responsible for listening to any bubbled event from the type of elements you have specified (any element with class notInTheMonth)

Check this

EDIT

If you build the calendar and set event listeners, every time, make sure that the calendar has finished it’s building process and then attach the event listeners.

1) Build the calendar

2) Make sure the calendar elements exist in the DOM and then attach listeners

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement