JavaScript Events Worksheet

Questions

Question 1

What is an 'event handler'?

An event handler is a line of code that runs when something happens, like clicking a button, moving your mouse, or etc.
Example:

			button.addEventListener("click", function(){
				alert("button has been clicked!");
			});
		
The event is a 'click', and the event handler will show an alert when a button has been clicked.

Question 2

How are event handlers invoked?

Event handlers are invoked automatically when the specified event occurs. As shown in question #1, the event handler is automatically triggered by the function you set.

Question 3

What is an 'anonymous' function?

An 'anonymous function' is a function that does not have a name. Instead of it being explicity named, it is used where it is needed. As shown in question #1, the 'function()' inside 'addEventListener' is anonymous because it has no name and is used on the spot.

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 attaches an eventListener to a DOM element. It listens for specified events that you set and runs a function in response that you create.

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