Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Reacting to Clicks

So far, your scripts have done everything at once, when the page loaded. But a page becomes interactive when it reacts to what the user does. Things that happen on a page, such as a click, a key press or typing into a field, are called events.

A button is the button element, for example <button id="hello">Say hello</button>. To make it do something, tell it which function to run when it's clicked:

const helloButton = document.querySelector("#hello");
const message = document.querySelector("#message");

helloButton.addEventListener("click", function () {
    message.textContent = "Hello!";
});

The method addEventListener() takes two arguments: the name of the event in quotes ("click") and a function. The function has no name here; it's written right where it's needed. Notice how the code ends: } closes the function, ) closes the parentheses of addEventListener(, and then comes the semicolon.

When the page loads, the script only hands the function over to the button; it doesn't run it. The function runs later, every time someone clicks the button: on the first click, on the second, and so on. Such a function is called an event handler.

The handler can also be an arrow function:

helloButton.addEventListener("click", () => {
    message.textContent = "Hello!";
});

Or a function you've defined before, passed by its name:

function sayHello() {
    message.textContent = "Hello!";
}

helloButton.addEventListener("click", sayHello);

Watch out for a typical mistake: addEventListener("click", sayHello()). With the parentheses, sayHello() is called right away, when the page loads, and addEventListener() gets what the function returned (undefined) instead of the function. There's no error, but the message appears before anyone clicks, and clicking does nothing. Pass the function without parentheses: the browser calls it itself, on every click.

Instructions

  1. 1

    When greetButton is clicked, change the text of message to Nice to meet you!. Use addEventListener() with function () { ... }. Then try the button in the page.

  2. 2

    When darkModeButton is clicked, toggle the class dark on page (the body element) with classList.toggle(). Write the handler as an arrow function this time. The first click makes the page dark, the next one light again.

Back

5/10

  1. 1. JavaScript in a Web Page
  2. 2. Finding Elements
  3. 3. When Nothing Is Found
  4. 4. Changing Styles
  5. 5. Reacting to Clicks
  6. 6. A Click Counter
  7. 7. Text Fields
  8. 8. Creating Elements
  9. 9. Showing an Array
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.