adding an event listener to an element that doesn 27t exist yet

Solutions on MaxInterview for adding an event listener to an element that doesn 27t exist yet by the best coders in the world

showing results for - "adding an event listener to an element that doesn 27t exist yet"
Eren
12 Jan 2021
1// You need to add the event listener when the element is created.
2function createEventListener() {
3  	var node = document.createElement('div');
4    if (node) {
5        node.addEventListener('click', function() {
6            console.log('Clicked Node');
7        });
8    } else {
9            console.error('No node');
10    }
11}