Event Handling in JavaScript
Event Handling in JavaScript
Events are basically generated by the user when the user tries to do some activity on our webpage.
A list of Some JavaScript Events are:-
- click
- dblclick
- mouseover
- keypress
- load
- unload
- keyup
- keydown
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);
Parameters explanation:-
- The first parameter is the event's type (like "click" or "dblclick").
- The second parameter is the callback function which gets executed.