JavaScript Events Worksheet
Questions
Question 1
What is an 'event handler'?
An event handler is a function that triggers upon a certain event happening on the webpage, usually some sort of mouse interaction.
Question 2
How are event handlers invoked?
First define a function, then hook up the function to a particular event.
Question 3
What is an 'anonymous' function?
A function with no name. They're only usable on the event they're tied to, and can't be called elsewhere in the code.
Question 4
Explain what the addEventListener() method of a DOM element is, and explain the purpose of each of it's parameters.
"addEventListener()" is hooked up to an object and waits for a defined event, then triggers some action or response.
e.g. button.addEventListener("click", function(){console.log("pressed")});
In the example, a button on the webpage is hooked up to the method, then waits for it to be clicked, upon which the string "pressed" will be console logged.
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