101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    JavaScript 101

    Receive aemail containing the next unit.
    • Introduction to JavaScript
      • 1.1What is JavaScript?
      • 1.2Setting up your environment
      • 1.3Basic syntax and variables
    • Data Types and Variables
      • 2.1Understanding data types
      • 2.2Variables and constants
      • 2.3Operators
    • Control Structures
      • 3.1Conditional statements
      • 3.2Loops
      • 3.3Error handling
    • Functions
      • 4.1Defining functions
      • 4.2Function expressions
      • 4.3Arrow functions
    • Objects and Arrays
      • 5.1Understanding objects
      • 5.2Understanding arrays
      • 5.3Array methods
    • Document Object Model (DOM)
      • 6.1What is the DOM?
      • 6.2Selecting and manipulating elements
      • 6.3Event handling
    • Asynchronous JavaScript
      • 7.1Callbacks
      • 7.2Promises
      • 7.3Async/Await
    • API Interaction
      • 8.1What is an API?
      • 8.2Fetch API
      • 8.3Working with JSON
    • Debugging
      • 9.1Console methods
      • 9.2Debugging tools
      • 9.3Common JavaScript errors
    • JavaScript Libraries
      • 10.1Introduction to libraries
      • 10.2Using jQuery
      • 10.3Using lodash
    • JavaScript Frameworks
      • 11.1Introduction to frameworks
      • 11.2Using React
      • 11.3Using Vue.js
    • Testing and Deployment
      • 12.1Writing tests with Jest
      • 12.2Deployment with Netlify
      • 12.3Continuous integration
    • Final Project
      • 13.1Project proposal
      • 13.2Project development
      • 13.3Project presentation

    Document Object Model (DOM)

    Understanding Event Handling in JavaScript

    high-level programming language

    High-level programming language.

    Event handling is a fundamental aspect of JavaScript and web development. Events are actions or occurrences that happen in the system you are programming, which the system tells you about so you can respond to them in some way if desired. In the context of the web and JavaScript, events are typically user interactions such as clicking a button, scrolling the page, or pressing a key on the keyboard.

    Different Types of Events

    There are numerous types of events that can be detected in JavaScript. Some of the most common include:

    • click: Fires when a mouse click is detected.
    • load: Fires when a page or an image has finished loading.
    • mouseover: Fires when the mouse pointer moves over an element.
    • mouseout: Fires when the mouse pointer moves out of an element.
    • keydown: Fires when a key is pressed down.
    • keyup: Fires when a key is released.

    Adding and Removing Event Listeners

    Event listeners are functions that wait for an event to occur. They can be added to any DOM object, not only HTML elements. Here is an example of adding an event listener to a button:

    let button = document.querySelector('button'); button.addEventListener('click', function() { alert('Button was clicked!'); });

    In this example, the addEventListener method is used to attach a click event to the button. When the button is clicked, the function is executed, and an alert box appears.

    Event listeners can also be removed using the removeEventListener method. This requires that the function passed in addEventListener has a name:

    let button = document.querySelector('button'); function showAlert() { alert('Button was clicked!'); } button.addEventListener('click', showAlert); button.removeEventListener('click', showAlert);

    Event Propagation: Capturing and Bubbling

    Event propagation is the process by which an event triggers its listeners, first on the window, then on descendant nodes, and finally on the target node itself. There are two phases of event propagation: capturing and bubbling.

    In the capturing phase, the event goes down the DOM tree, from the window to the target element. In the bubbling phase, the event bubbles up from the target element to the window.

    By default, event handlers are executed in the bubbling phase. However, you can set an event handler to execute in the capturing phase by setting the third argument of addEventListener to true.

    Using the Event Object

    When an event is fired, information about the event is passed to the event handler function in the form of an event object. This object contains properties and methods related to the event. For example, the target property can be used to get the element that triggered the event:

    button.addEventListener('click', function(event) { console.log(event.target); });

    Preventing Default Behavior

    Some events have a default action associated with them. For example, clicking a link navigates to a new URL, and submitting a form sends the form data to the server. You can prevent these default actions using the preventDefault method of the event object:

    let link = document.querySelector('a'); link.addEventListener('click', function(event) { event.preventDefault(); console.log('Link was clicked, but no navigation occurred'); });

    In this example, clicking the link will not navigate to a new URL, but instead will log a message to the console.

    Test me
    Practical exercise
    Further reading

    Buenos dias, any questions for me?

    Sign in to chat
    Next up: Callbacks