Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    JSJavaScript

    Getting started

    • What is JavaScript?
    • Variables: let, const and var
    • Data types
    • Type conversion

    Operators and strings

    • Arithmetic and assignment
    • Comparison and logic
    • Strings and template literals
    • String methods
    • Numbers and Math

    Control flow

    • Conditions: if and else
    • switch and the ternary operator
    • Loops: for and while
    • Challenge: FizzBuzz

    Functions

    • Functions
    • Arrow functions
    • Parameters: default, rest and spread
    • Scope and closures
    • Callbacks and higher-order functions

    Arrays

    • Array basics
    • map, filter and find
    • reduce and sorting
    • Destructuring

    Objects

    • Object basics
    • Copies and references
    • Methods and this
    • JSON and optional chaining

    Classes and OOP

    • Classes
    • Inheritance

    JavaScript in the page

    • Controlling the page (DOM)
    • Creating elements
    • Events
    • Forms and validation
    • Saving in the browser (localStorage)

    Errors and async

    • Handling errors: try and catch
    • Timers and the event loop
    • Promises
    • async and await
    • Fetching data from an API

    Practical tools and techniques

    • Searching with regular expressions
    • Cancellation and stale results
    • Testing and debugging JavaScript

    Modern JavaScript and a project

    • Map and Set
    • Dates and localised formatting
    • Modules: import and export
    • Project: a complete to-do app
    JavaScript

    JavaScript in the page · Lesson 31 of 45

    Events

    Make the page react when someone clicks or types.

    9 minutes2 games

    el.addEventListener("click", fn) tells the browser: “when this event happens on this element, run this function.” The function receives an event object with details: e.target (the element), e.key (the key pressed).

    EventFires when

    click

    you click

    input

    a field changes (every keystroke)

    change

    a field's change is committed

    submit

    a form is submitted

    keydown

    a key is pressed

    mouseover / mouseout

    the pointer enters/leaves

    HTML
    <button id="btn">Clicked 0 times</button>
    <p><input id="name" placeholder="Type your name"></p>
    <p id="hello"></p>
    
    <script>
      let count = 0
      const btn = document.querySelector("#btn")
      btn.addEventListener("click", () => {
        count++
        btn.textContent = `Clicked ${count} times`
      })
    
      document.querySelector("#name").addEventListener("input", (e) => {
        document.querySelector("#hello").textContent = "Hi " + e.target.value
      })
    
      document.addEventListener("keydown", (e) => console.log("key:", e.key))
    </script>

    Click the button, type in the box, and watch the console.

    Event delegation

    Events travel up the tree (bubbling): a click on an li also reaches its ul. So instead of a listener per item, put one on the parent and inspect e.target. It even works for items added later.

    HTML
    <ul id="menu">
      <li data-page="home">Home</li>
      <li data-page="courses">Courses</li>
      <li data-page="about">About</li>
    </ul>
    
    <script>
      document.querySelector("#menu").addEventListener("click", (e) => {
        const li = e.target.closest("li")
        if (li) console.log("Go to", li.dataset.page)
      })
    </script>

    Important

    A form's submit reloads the page by default. Call e.preventDefault() first in your handler to stop that and handle the data yourself.

    Interview question

    Explain event bubbling and event delegation.

    Order the lines

    Order it so clicking the button logs clicked.

    1. Click the lines in the right order

    Quick question

    Which event fires on every character typed into a field?

    Learn more from

    • javascript.info: Introduction to browser eventsjavascript.info
    • javascript.info: Bubbling and capturingjavascript.info
    • javascript.info: Event delegationjavascript.info
    • MDN: EventTarget.addEventListener()developer.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousCreating elementsNextForms and validation
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.