Skip to main content

Day 5 - Buttons and Event listeners

1. New Achievements

Cordelle has shared this Playlist of Javascript exercises

  • Can these be used as an Achievement?

2. Functions and events

Slides - Javascript events and callbacks

Demo: Refactor of yesterday’s demo

Activity

In groups of 2-3, refactor a code example to use an click Listener and callback function so that the code runs only when a button is clicked:

  1. Create an index.html page with embedded or external Javascript;
  2. Add one of the following code examples to your page so that it runs when the page first loads:
  3. Move the code into a new function named handleClick so that it no longer runs on page load;
  4. Add an HTML <button> to your HTML page;
  5. Create a button DOM variable using document.querySelector();
  6. Add a click Event Listener to your button so that it runs handleClick() when the button is clicked;
  7. Bonus: Add a prompt() to your function code to accept user input and present a customized response for your click Event.

Lab Time

  • Early Dismissal!
  • Tony will be available for the rest of the afternoon but no attendance will be taken.

Prep

Review

Reading list

Watch list