Sitemap

JavaScript’s Interaction with Events

3 min readFeb 5, 2019

--

Press enter or click to view image in full size

What is an Event?

HTML events, simply put, are things that can occur on a web page. What do I mean by “things”? I mean everything.

While that seems like a pretty broad statement, looking at the documentation on events, you’ll see that it really does cover pretty much everything that can happen on a web page. Here are some of the most common event types & names:

Mouse Events: mousedown, mouseup, click, dblclick, mousemove, mouseover, mousewheel, mouseout, contextmenu

Touch Events: touchstart, touchmove, touchend, touchcancel

Keyboard Events: keydown, keypress, keyup

Form Events: focus, blur, change, submit

Window Events: scroll, resize, load, unload

So what?

So why do these events matter? Well, it turns out that JavaScript (among other languages) is able to listen for these events and respond to them. Let’s explore what that means.

JavaScript’s Interaction with HTML Events

JavaScript responds to events through event handlers. These are essentially blocks of code or functions that are triggered when an event occurs.

Event handlers use event listeners, which listen for specific occurrences of an event, then run the code or function in response to the event.

3 Types of Event Handlers

JavaScript offers three different ways to register event handlers:

I. Event Handler Properties

const btn = document.querySelector('button')btn.onclick = function() {
alert('Using Event Handler Property!')
}
Press enter or click to view image in full size

In this example, onkeypress is the event handler property that is being used. Event handler properties are special types of properties that, when set equal to some code, will be run when the event fires.

Get Sarah’s stories in your inbox

Join Medium for free to get updates from this writer.

Event handler properties can be used by taking the event name (in this case, keypress) and prepending it with ‘on’.

II. HTML Event Handler Attributes (or Inline Event Handlers)

<button onclick="alert('This is an old-fashioned event handler!')">Click Me</button>
Press enter or click to view image in full size

As you can see, for HTML event handler attributes, the attribute value is the literal JavaScript code that you want to run when the event is triggered.

This style of event handlers is written within HTML, and it’s never a good idea to mix your HTML and JavaScript. This was the first way that JavaScript used event handlers, but is now outdated due to its many limitations.

III. addEventListener()

const btn = document.querySelector('button')function handler1(){
alert('Thanks!');
}
function handler2(){
alert('Thanks again!');
}
btn.addEventListener('click', handler1);
btn.addEventListener('click', handler2);
Press enter or click to view image in full size

This is the modern way to write your event handlers. The addEventListener() function takes in two parameters: 1) the name of the event we want to register the handler for, and 2) the code with the handler function that we want to run in response to the event being triggered.

Note: you could also put all of the code inside the addEventListener() function using an anonymous function like so:

const btn = document.querySelector('button')btn.addEventListener('click', function() {
alert('Thanks!')
});
btn.addEventListener('click', function() {
alert('Thanks again!')
});

--

--