Event in Javascript
Event in Javascript
What is an event?
Events are things that happen in the system you are programming, which the system tells you about so
your code can react to them. — the system produces (or "fires") a signal of some kind when an event
occurs, and provides a mechanism by which an action can be automatically taken (that is, some code
running) when the event occurs. Events are fired inside the browser window, and tend to be attached to
a specific item that resides in it. This might be a single element, a set of elements, the HTML document
loaded in the current tab, or the entire browser window. There are many different types of events that
can occur.
For example:
• The user selects, clicks, or hovers the cursor over a certain element.
• The user chooses a key on the keyboard.
• The user resizes or closes the browser window.
• A web page finishes loading.
• A form is submitted.
• A video is played, paused, or ends.
• An error occurs.
Events are fired to notify code of "interesting changes" that may affect code execution. These can arise
from user interactions such as using a mouse or resizing a window, changes in the state of the
underlying environment (e.g. low battery or media events from the operating system), and other causes.
Each event is represented by an object that is based on the event interface, and may have additional
custom fields and/or functions to provide information about what happened.
In JavaScript, event handling is a fundamental part of creating interactive web applications. It allows
developers to define code that will be executed when a particular event occurs.
To react to an event, you attach an event handler to it. This is a block of code (usually a JavaScript
function that you as a programmer create) that runs when the event fires. When such a block of code is
defined to run in response to an event, we say we are registering an event handler. Note: Event handlers
are sometimes called event listeners — they are pretty much interchangeable for our purposes, although
strictly speaking, they work together. The listener listens out for the event happening, and the handler is
the code that is run in response to it happening.
Note: Web events are not part of the core JavaScript language — they are defined as part of the APIs
built into the browser.
There are various types of events in JavaScript, such as mouse events (click, hover, etc.), keyboard
events (keypress, keyup, etc.), form events (submit, change, etc.), and many others.
//html code
<button id="myButton">Click Me</button>
JavaScript code.
//Javascript code
In bubbling the inner most element's event is handled first and then the outer: the <p> element's click
event is handled first, then the <div> element's click event.
In capturing the outer most element's event is handled first and then the inner: the <div> element's
click event will be handled first, then the <p> element's click event.
With the addEventListener() method you can specify the propagation type by using the "useCapture"
parameter:
addEventListener(event, function, useCapture);
The default value is false, which will use the bubbling propagation, when the value is set to true, the
event uses the capturing propagation.
Example below:
<!DOCTYPE html>
<html>
<head>
<style>
#myDiv1, #myDiv2 {
background-color: coral;
padding: 50px;
}
#myP1, #myP2 {
background-color: white;
font-size: 20px;
border: 1px solid;
padding: 20px;
}
</style>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<h2>JavaScript addEventListener()</h2>
<div id="myDiv1">
<h2>Bubbling:</h2>
<p id="myP1">Click me!</p>
</div><br>
<div id="myDiv2">
<h2>Capturing:</h2>
<p id="myP2">Click me!</p>
</div>
<script>
document.getElementById("myP1").addEventListener("click", function() {
alert("You clicked the white element!");
}, false);
document.getElementById("myDiv1").addEventListener("click", function() {
alert("You clicked the orange element!");
}, false);
document.getElementById("myP2").addEventListener("click", function() {
alert("You clicked the white element!");
}, true);
document.getElementById("myDiv2").addEventListener("click", function() {
alert("You clicked the orange element!");
}, true);
</script>
HTML DOM Events