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
click | When the user clicks on an element. |
dblclick | When the user double clicks on an element. |
mousedown | On mouse click down. |
mouseup | On mouse click up. |
mouseenter | As the mouse cursor enters the element. |
mouseover | When the mouse cursor hovers over the element. |
mousemove | As the mouse cursor moves over the element. |
mouseleave | When the mouse cursor exits the element. |
wheel | When 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.
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.
keypress | When the user types something. |
keyup | On key press up. |
keydown | On key press down. |
Similar to mouse events, key press events will also return some parameters – Check it out at MDN.
PAGE EVENTS
load | When the page loads |
scroll | On page scroll |
resize | When 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:
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!
The post Javascript Events 101 – The Basics appeared first on Code Boxx.