Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Javascript Events 101 – The Basics

Learn Javascript

Introduction

The Basics

JS In HTML Variables & Data-Types Operators Arrays Loops Conditions Functions Events

JS & HTML

DOM

More

Object-Oriented JS

INTRODUCTION
EVENTS HANDLING

Welcome to a guide on Javascript Events. In this chapter of the beginner’s series, we will be exploring how to handle events in Javascript – Everything from a mouse click to key presses to when a page loads. Read on.

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!

WHAT THE HECK ARE EVENTS?

Events in Javascript are used to notify the code whenever “something interesting has happened” – That can be everything from a mouse click to a page being loaded. Based on these events, we can then write code to react accordingly. For example, we can run a hello function when a user clicks on a button by defining an “onclick” attribute:

EVENT LISTENER

Alternatively, you can also attach an event listener to the HTML elements using Javascript itself.

As you may have noticed, when we attach the Javascript event listener, we simply define the corresponding event without the “on” keyword.

MOUSE EVENTS

clickWhen the user clicks on an element.
dblclickWhen the user double clicks on an element.
mousedownOn mouse click down.
mouseupOn mouse click up.
mouseenterAs the mouse cursor enters the element.
mouseoverWhen the mouse cursor hovers over the element.
mousemoveAs the mouse cursor moves over the element.
mouseleaveWhen the mouse cursor exits the element.
wheelWhen the mouse wheel turns.

Please do take note that some events do provide some parameters, the mouse move, for example, provides the X and Y mouse coordinates.

Tracking mouse coordinates

For a complete list of mouse events and parameters, you can check out MDN Web Docs.

KEYBOARD EVENTS

Keyboard events are mostly useful for text input elements. For example, you can run an auto-complete as the user types something.

keypressWhen the user types something.
keyupOn key press up.
keydownOn key press down.

Similar to mouse events, key press events will also return some parameters – Check it out at MDN.

Tracking key press

PAGE EVENTS

loadWhen the page loads
scrollOn page scroll
resizeWhen the page is resized

Events don’t just cover user input, they can also be fired on certain processes or timing, for example, when the page loads:

upload.php

CREATING YOUR OWN CUSTOM EVENTS

Yes, you can. You can create your own events and fire them, for example, upon finishing a certain process:

function dummy(){
  // Do your processing
  var x = 10;
  var y = 15;
  var z = x + y;

  // Custom event - finished calculation
  var evt = new CustomEvent('dummyDone');
  window.dispatchEvent(evt);
}

window.addEventListener("dummyDone", function(){
	alert("Dummy Done!");
});
dummy();

CLOSING
WHAT’S NEXT?

We have come to the end of this short chapter, and I hope it has helped you to better understand Javascript functions. If you have questions, please feel free to comment below. Happy coding!

Common Javascript Events (click to enlarge)


JS Functions DOM

The post Javascript Events 101 – The Basics appeared first on Code Boxx.



This post first appeared on Xxxxxxxxx, please read the originial post: here

Share the post

Javascript Events 101 – The Basics

×

Subscribe to Xxxxxxxxx

Get updates delivered right to your inbox!

Thank you for your subscription

×