Skip to content
Robert Thomas edited this page Apr 28, 2015 · 1 revision

I've been asked a couple times, "How do we start the homework?" The instructions start off with "When I click..." That's an indication that we should be doing something with a click event. We can't modify the HTML, so we'll have to use addEventListener to attach a click event to the button.

The reason the instructions hint about event.preventDefault() is that the default click action for <button></button> is to reload the page (as if it was submitting a form). So the event listener you add to <button> won't work, because the page will be reloading.

Finally, I really like putting all my JS in the <head>. However, that requires an onload attribute on <body>. The <body> in this HTML doesn't have onload, and we're not allowed to touch the HTML. So we'll need to rely on the fact that the JS has been included at the end of <body>.

So your code should start off something like this:

  1. Get the button element
  2. Add a click event listener to it
  3. Prevent the default click event from happening
  4. Continue on to add the new stuff you want to happen
Clone this wiki locally