DEV Community

JavaScript Event Guides

A JavaScript Event, in a web dev context, is how a lot of interactivity flows. Sometimes an event is abstracted away by the framework, and sometimes you listen directly. No matter what, hopefully you find these guides helpful!

This is a collection of top and trending guides written by the community on subjects related to JavaScript Event concepts. For all things JavaScript, check out the JavaScript tag! Please contribute more posts like this to help your fellow developer in need.

How to Programmatically Trigger 'Click' Event in Angular 4 & 5

HTMLElement represents any element in the document and implements Event Handler interface, so now we can call click(). Successfully, the file browser is triggered to open.


Resize event listener using React hooks

There is one more thing to mention, resize event it is triggered multiple times while the user is actively dragging the browser's window resize handle, it may affect the performance of your application if you have a complex logic in the resize listener. One way to deal with it is to implement some debounce mechanism that will limit the rate at which a function can fire.


✨♻️ JavaScript Visualized: Event Loop

Oh boi the event loop. It’s one of those things that every JavaScript developer has to deal with in one way or another, but it can be a bit confusing to understand at first. I’m a visual learner so I thought I’d try to help you by explaining it in a visual way through low-res gifs because it's 2019 and gifs are somehow still pixelated and blurry.


Function identity in JavaScript, or how to remove event listeners properly

Some DOM elements are queried for and event listeners are being attached to each.


Handle Stack Navigator's event in current screen with React Navigation

In this situation, we may want to handle the press event in Screen Component, not in Navigation Action.

This is because React Navigation should focus on handling navigation so events and logic on the screen should be handled by the screen Component.


Click Event Filtering on a JSON Rendered List in Vue.js

In this tutorial, we'll explore basic click event filtering on a rendered list using JSON data in Vue.js. This is a basic tutorial, but it should be a solid foundation to start building something more complex.


Google Maps: Show multiple markers and add your event on click

We create a variable responsiveZoom that checks the window size and give a zoom level accordingly. We add an event listener on window resize that zooms in or out on certain breaking points.


Adding Event Listeners to the future DOM elements using Event Bubbling

It all started when I was developing a simple MVC to-do app using vanilla javascript. I came across a requirement of listening to the click event of the task item that is going to be added to the UI when the user clicks on 'Add Task Button'.


Node.js Under The Hood #3 - Deep Dive Into the Event Loop

Now we're going to enter another realm, the realm of the event loop and understand why all JS runtimes and all JS engines provide one of those. First, let's get to the core of it.


How to add an event listener to multiple elements in JavaScript

In JavaScript you add an event listener to a single element using this syntax:


Understanding the Node.js event loop phases and how it executes the JavaScript code.

I believe if you are reading this, you must have heard about the famous event loop that Node.js has, how it handles the concurrency mechanism in Node.js and how it makes Node.js a unique platform for event driven I/O. Being an Event driven I/O, all of the code that is executed is in the form of callbacks. Hence, it is important to know how and in what order are all these callbacks executed by the event loop. From here onwards, in this blog post, the term 'event loop' refers to the Node.js' event loop.


The .native Event Modifier In Vue.js

Easy event handling is one of the most prominent features of Vue.js. However, there are some event handling concepts that I wish someone would have shared with me from day one!


Should I use event delegation in React?

Event delegation is a performance optimization method in the world of Javascript. Let's say you have an unordered list <ul> with 1000 list items, and you want to do something each time a list item is clicked. With event delegation approach, instead of adding one event listener to each of the child items, you only add 1 event listener to the parent <ul>. It is a neat approach. You can read more about it here.


The JavaScript Execution Context, Call-stack & Event Loop

This article is about deepening our knowledge of JS by understanding how our JS code gets executed. These laws are governed by the interaction of the Execution Context, Call-stack and Event Loop. The interplay of these three concepts is what allows our code to be executed. A good understanding of these foundational concepts is crucial in order to understand more advanced stuff such as scopes and closures. Let's step right in.


Difference between the Event Loop in Browser and Node Js?

Every JS developer must have heard of the term Event Loop. Both JS and Node Js is based on the principle of event loop which has similarities and dissimilarities to some extent. Let's discuss the event loop in brief and find the difference between them. 📖


Trigger an onClick event outside the child element, works for nested child components too.

1. Add click eventListener to the parent div

Assign a id to the parent and top-level child div element, if not already. On componentDidMount() of the child element, add a click event listener as shown in the below code.

image


JavaScript For Loop Click Event ← Issues & Solutions Explained

When you attach multiple click events to elements such as buttons inside a for loop, the click event will always give us the last index value regardless of what button is pressed.


Listen for and debounce Window Resize Event in Vue.JS

Learn how to listen for a resize event in Vue and how to debounce the handler function to improve performance.


Implementing Event Sourcing and CQRS pattern with MongoDB

Event Sourcing and CQRS are quite simple concepts. But they are often made complex to understand because of overly complex implementations. Implementing Event Sourcing with CQRS in a simple application has its benefits, and it can be done without compromising the simplicity and maintainability of the application. And in this article, let’s see a practical implementation of Event Sourcing and CQRS using MongoDB Views.


A Bit about Event Delegation in React

In this article, we will consider event delegation and what it is for. How it can help you in understanding the mechanisms of the framework, and maybe even in improving your decisions in pure JS.


What is Event loop in JavaScript?

Let's look into JavaScript Event loop from a different angle. Sounds interesting, right? It is! So, sit tight and let's dive into it!

The concept of event loop is very simple. But in order to understand that, first we need to be clear about some concepts related to event loop.


How to Send a Custom Event to Google Analytics 4 Using JavaScript

This article is a quick demo of how you can send a custom event to Google Analytics 4 (GA4) using just pure JavaScript.


Node.js Event Emitter

Node's event-driven architecture allows us to execute certain actions when something happens. This is done via objects (called "emitters") which can emit named events that cause functions ("listeners") to be executed. Objects that emit events are instances of node's EventEmitter class, made available via the events module. In this article we'll look at node's event emitter.


Quick Javascript Tips 1: Easiest way to get enter key event with jQuery.

But don't despair! Here’s the thing: 229 is the key event that the browser emits when it’s processing what the user typed, and that happens a lot on Android because of auto-correct.


Simple explanation of event.currentTarget, event.target and event delegation

Javascript provides something called event listeners. Its main objective is the name itself, it listens for events and when the event occurs then something happens. In other words when an event like a "click" or a "mouseover" is triggered then a specified written function is executed, this is where the addEventListener() method comes in.


AWS SNS & SQS Event Bus

Doing this all in one function is fine but we can do better with an event bus.


How to Handle Swipe Event on React Carousel

Now we will add a simple swipe event for touchscreen devices, this is particularly handy for mobile devices like smartphone to make it easier to navigate with 1 finger.


Vue.js 101 - Event object, v-for and v-bind

Each of those events have an attach event object attach to the method.


Node.js por Baixo dos Panos #3 - Um Mergulho no Event Loop

Agora vamos entrar em outra parte, a parte do Event Loop e entender por que todos os runtimes JS e todos os engines JS tem um desses. Primeiro, vamos entender o core disso tudo.


JS illustrated: The event loop 🔁

But how is it possible? The event loop makes it possible!


I put 18 event handlers on a React element

In a technical sense a syntheticEvent is a wrapper that's part of React. It takes the place of the native event handlers you might know from plain Javascript.


Everything about Event Handlers and Event Propagation

There are 3 ways to attach a handler to an event in JavaScript.


Building a client side router in React with event hooks (pt1: events)

The browser gives us an onpopstate event when the user navigates using the buttons, but there is no event for the navigation to a new URL so we are going to have to deal with that ourselves.


Extending Javascript array with an event handler

Now, say we wish to find the oldest age and the sum of all ages. This can easily be done using a loop and some variables, but for the sake of this article I will use an event listener attached to the traditional javascript array to pull this off.


How To Trigger a Scroll Event in Vue.js

At first, I used the scroll event in JavaScript, but on the performance side, I realized that it was not a great way to trigger an event every time the user scrolled the page.


Vue Event And Key Modifiers

Event and Key modifiers allow you to add event functionality directly inline where you define your template. This can help you write cleaner and more accessible code to your site with minimum effort.


Make your own state management for React with Proxies and Event emitters

Using CustomEvents and dispatching to the DOM works as well when using pure React. However in scenarios where the DOM is inaccessible (SSR or SSG using Nextjs for example), that could not be an option. Also events from event emitters have less dead-weight since they do not propagate or bubble anywhere.


Under the hood of event listeners in React

Recently, during the migration to React 17, I had a problem between event listeners handled by React and one added on document manually. It was due to the part Changes to Event Delegation of the React 17 release note.


An overview of Event Loop, Tasks, and Microtasks

The task is code to be executed until completion. For each turn of the event loop, one task is executed. A task can schedule other tasks (asynchronous in nature). Multiple task queues are maintained by the browser.


Metamask: when does actually accountChanged event emits?

Still eth_accounts or eth_requestAccounts will provide you only one account. I will share two scenarios where this event will emit.


How asynchronous works in JavaScript. Event Loop concept

Event Loop is one of those things that every JavaScript developer encounters in one way or another. JavaScript provides a concurrency model based on an event loop, which executes the code, collecting and processing events, and performs queued sub-tasks.


How To Create A Simple Alert For An onbeforeunload Event (Closing Browser or Navigating Away From Page) [updated]

An onbeforeunload event occurs when the document (the current page of your site/application) is about to be unloaded (closed/exited). A example of this would be someone closing the browser window or clicking a link to a different page.


How I Created an Event-Driven Backend with RxJS, Server-Sent Events, Express and NodeJS

To do so, let's define a function called EmitTickerPrice. Every time we get a new ticker price data, we will call this method with the proper parameters, and it will emit a new event to our ticker subject.


Strongly typed event emitters using EventTarget in TypeScript

In case you weren't aware, you can have an event emitting class using only natively available APIs:


Node.js Event Loop In Action

In my last post(Deep dive into Node.js Architecture), we learnt about the internals of Node.js and how multiple asynchronous operations work efficiently in a single-threaded environment. We also talked about how event loop works and facilitates the event-driven architecture of Node.js. I would recommend going through my previous post before reading this one.


TIL - Vue.js Event Bus more clearly

Vue.js event bus technique is sometime useful.


MutationObserver and Event Usage

I wrote createAndTriggerEvent function for creating a new Event and triggering it. When our data is ready we're triggering it.


Happy JavaScript Event coding!