JavaScript Events Worksheet
Questions
Question 1
What is an 'event handler'?
An event handler is a piece of code that waits for something to happen, like when you click a button or press a key. When that action (or "event") happens, the event handler does its job.
Question 2
How are event handlers invoked?
Event handlers are triggered automatically when something specific happens, like clicking a button or pressing a key.
Here’s how it works:
Connect the Event: You link the event handler to an action, like a button click.
Action Happens: When you perform that action (like clicking the button), the computer notices.
Event Handler Runs: The event handler runs automatically and does what it’s supposed to (like showing a message).
Question 3
What is an 'anonymous' function?
An anonymous function is a function without a name. It’s a small, one-time-use function often written where it’s needed, rather than being defined separately.
nameless function for doing small tasks without needing to call it by name later.
Question 4
Explain what the addEventListener() method of a DOM element is, and explain the purpose of each of it's parameters.
The addEventListener() method is used to set up a function that will run when a specific event occurs on a DOM (Document Object Model) element, like a button, link, or input field. This method lets you add multiple event listeners to an element, meaning you can set up different actions for different events (or even for the same event type).
Coding Problems
Coding Problems - See the 'script' tag below this h3 tag. You will have to write some JavaScript code in it.
Always test your work! Check the console log to make sure there are no errors.
Orange
Green