events

Events in JavaScript

Events in JavaScript

Events are actions that can be detected by JavaScript, such as clicks or key presses.

Event Listeners

Add an event listener to an element to respond to events:

      
        document.getElementById("myButton").addEventListener("click", function() {
          alert("Button was clicked!");
        });
      
    

Common Events

Activity

Try It Yourself!

Create an input field and button. When the button is clicked, display an alert with the input's value.

Quiz

Quick Quiz

  1. What is an event in JavaScript?
  2. How do you add an event listener?
  3. What does the "click" event do?

Answers: An action like a click; addEventListener; detects clicks.