Event handlers
Event Handlers
3 ways to add event handler to an HTML element:
using the HTML attribute e.g.
onclick
oronmouseover
Using DOM in JS
*note: here we don't use parenthesis with the method name( not
makePayment()
, justmakePayment
)To remove the handler,
Using
addEventListner
method - allows to add multiple eventListeners by calling the method multiple times on the elementWith this, we can remove the event listener using
removeEventListner
method:
Event Object
On occurrence of an event, the browser would create an Event
object and pass it as first argument to the event handler function. This Event
object carries details of the event, it would have properties depending on the type of the event, below are some common properties
type
: type of the event -click
orkeyup
ormouseout
etctarget
: See in event bubblingcurrentTarget
: See in event bubblingclientX/clientY
: pixel coordinates of the mouse position that triggered the event if its a mouse event.
Last updated