Sitemap

JavaScript Events

3 min readFeb 16, 2020
Press enter or click to view image in full size
Photo by Adam Solomon on Unsplash

JavaScript’s interaction with HTML is handled through events that occur when the user or the browser manipulates a page. Events are actions that happen in the system and to which programers can respond in some way.

Events are a part of the Document Object Model (DOM), they are fired inside the browser window, and tend to be attached to a specific item that resides in it — a single element, set of elements, the HTML document loaded in the current tab, or the entire browser window.

Browsers do this by allowing us to register functions as handlers for specific events.

In this example, the window binding refers to a built-in object provided by the browser. It represents the browser window that contains the document. Calling its addEventListener method registers the second argument to be called whenever the event described by its first argument occurs.

Events and DOM nodes

Each browser event handler is registered in a context. We can attach a handler through the attribute whose name is the event name with on in front of it.

Get Gia Edgington’s stories in your inbox

Join Medium for free to get updates from this writer.

But a node can have only one onclick attribute, so we can register only one handler per node that way. The addEventListener method allows us to add any number of handlers so that it is safe to add handlers even if there is already another handler on the element.

Press enter or click to view image in full size

Event Objects

Sometimes inside an event handler function there will be a parameter specified with a name such as event, or e. This is called the event object, and it is automatically passed to event handlers to provide extra features and information.

Propagation

For most event types, handlers registered on nodes with children will also receive events that happen in the children. If a button inside a paragraph is clicked, event handlers on the paragraph will also see the click event.

But if both the paragraph and the button have a handler, the more specific handler — the one on the button — gets to go first. The event is said to propagate outward, from the node where it happened to that node’s parent node and on to the root of the document. Finally, after all handlers registered on a specific node have had their turn, handlers registered on the whole window get a chance to respond to the event.

At any point, an event handler can call the stopPropagation method on the event object to prevent handlers further up from receiving the event. This can be useful when, for example, we have a button inside another clickable element and we don’t want clicks on the button to activate the outer element’s click behavior.

Press enter or click to view image in full size

This example registers “mousedown” handlers on both a button and the paragraph around it. When clicked with the right mouse button, the handler for the button calls stopPropagation, which will prevent the handler on the paragraph from running. When the button is clicked with another mouse button, both handlers will run.

We can also use the target property that refers to the node where they originated.

--

--