Responding to Events: an overview

An event is something that happens on a page, such as a click, and your code responds to it by giving the browser a function to run each time it happens.

Apprentice · JavaScript · Updated

By

After this lesson you can

  • Attach an event handler in HTML with an attribute such as onclick

Key takeaways

  • An event is something that happens on a page, such as a click, a key press or a form being sent.
  • An event handler is a function the browser runs every time the event it's attached to fires.
  • In the declarative style, an HTML attribute such as onclick holds the code to run, so onclick="handleSubmit()" calls handleSubmit.

The browser hosting your web page runs a continuous event loop which "listens" for web page and user events. When an event is fired the browser notifies the page's JavaScript runtime of the event.

You can subscribe to these events by creating an event handler function. Once you subscribe to an event your function will be executed every time the event is fired.

There are two ways to subscribe to an event, through declarative and imperative programming styles.

Declarative

Declarative programming means using semantic HTML to describe what you want to happen. The following HTML registers a button's click event to be handled by a JavaScript function named handleSubmit().

function handleSubmit()
{
    console.log("submit button clicked");
}
<button id="submitButton" onclick="handleSubmit()">Submit</button>

The onclick attribute holds the code the browser runs each time the button is clicked. Here that code calls handleSubmit(), so the parentheses matter: onclick="handleSubmit" only names the function, and a click does nothing. The next lesson, Imperative, shows the other style.

Do it: write the functions the cart's buttons call

The snack stand's order page has two buttons, and their HTML already says what to run when each one is clicked:

<button id="add" onclick="addBag()">Add a bag</button>
<button id="empty" onclick="emptyCart()">Empty the cart</button>

Those two functions don't exist yet, so for now a click only puts an error in the Console.

What the starter gives you:

  • count: the span on the page that shows how many bags of trail mix are in the cart. It starts at 0.
  • bags: a variable holding that number, starting at 0.

For example, Add a bag clicked twice:

What happens bags The cart shows
The page loads 0 0
Add a bag is clicked 1 1
Add a bag is clicked again 2 2

Your job: write the two functions, named exactly as the onclick attributes call them.

  • addBag(): adds 1 to bags, then shows the new number in count.
  • emptyCart(): sets bags back to 0, then shows 0 in count.

The tests click the buttons in order, on the same page, so each one starts where the one before it stopped.

Practiced live on the lesson page: write the JavaScript right there and check it.

Check yourself

2 questions to try on the lesson page.

So what

Every button, link and form you've ever used on the web works by responding to events. Attaching a handler is how your page starts listening.

Key terms

Cheat sheets