Events are basically generated by the user when the user tries to do some activity on our webpage.
Adding Events to Elements:-
< element onclick="show()" >
In the example above, show() is the function called on the event click.
Adding Event by addEventListener() method:-
The other way is to use addEventListener(). In this, it attaches an event handler to an element without overwriting existing event handlers.
element.addEventListener(event, function, useCapture);
- The first parameter is the event's type (like "click" or "dblclick").
- The second parameter is the callback function which gets executed.