I\'m learning how to manipulate events in JavaScript and I\'m wondering \"why do you have to pass the event object as a parameter (argument) into a function when using event
Simply put, the Event object passed to a handler contains details about the event. For example, a KeyboardEvent contain info about the key pressed, the corresponding character, and any modifier keys (alt, shift, control, meta) that were held down.
Does the event handler try to pass in an event before it gets assigned or?
The handler is your function, so it's the receiver of event
, not the passer.
onclick
property (or by calling addEventListener, the modern, preferred method), which is before the handler is invoked.So, when a user clicks on your #button_1
, this causes a "click" event to fire on the button, which invokes the button's "click" handler, which is passed a MouseEvent.
For more information, read about event-driven programming.