Published
An event in JavaScript is like a signal that something has happened in the application. For instance, when a user clicks something, scroll the page, push a key, or load a page, an event is triggered. JavaScript provides an ability to catch these events and execute a code block when such events are detected.
Event handlers in JavaScript are used to manage how events are processed. You can attach an event handler to an HTML element to execute JavaScript code. There are three ways to attach an event handler: Inline HTML event attributes (such as onclick); Traditional event handler; or The addEventListener() method.
//Inline HTML event attributes
<button onclick="alert('Hello World!');">Click me</button>
//Traditional event handler
var btn = document.getElementById('myBtn');
btn.onclick = function(){ alert('Hello World!'); };
//addEventListener() method
var btn = document.getElementById('myBtn');
btn.addEventListener('click', function(){ alert('Hello World!'); });
Event propagation is the flow of events in the DOM. It's divided into three phases: capturing phase, target phase, and bubbling phase. The addEventListener() method can allow you to control which phase to trigger the event handler.
var parent = document.getElementById('parent');
var child = document.getElementById('child');
//Using addEventListener(), with 'true' to specify the capturing phase
parent.addEventListener('click', function(){ alert('Hello from parent!'); }, true);
child.addEventListener('click', function(){ alert('Hello from child!'); }, true);
The event object is passed to the event handler and provides information about the event. For example, you can get the element that triggered the event, the type of the event, and the coordinates of the mouse when the event was triggered.
var btn = document.getElementById('myBtn');
btn.addEventListener('click', function(event){
console.log(event.target); // The element that triggered the event
console.log(event.type); // The type of the event
console.log(event.clientX); // The X coordinate of the mouse when the event was triggered
});
Sometimes you might want to prevent the default behavior of an event. For instance, prevent a link from navigating to a new page, or prevent a form from submitting. This can be achieved using event.preventDefault().
var link = document.getElementById('myLink');
link.addEventListener('click', function(event){
event.preventDefault();
alert('The link will not navigate to a new page.');
});
By reading this article, you've invested 1.17 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
Events in JavaScript are like when mom asks you to clean your room. You know you have to do it, but it doesn't happen until you decide to start. Similarly, in JavaScript, an event could be when someone clicks on a button or presses a key on their keyboard. These are actions that we can make our program respond to.
Just like when your mom said you can have ice cream after you clean your room, we can tell JavaScript to do certain things after an event happens, like changing the color of a button when it is clicked. We do this by adding an 'event listener' to the element.
Event listeners in JavaScript are like instructions for what to do when a certain event happens. If we talk about the ice cream, your mom is 'listening' for the event of your room being clean, and when that happens, she responds by giving you ice cream.
button.addEventListener('click', function(){ button.style.backgroundColor = 'red'; });
In the example above, we used something called a function. It's like a special recipe to change the color of the button. Once we have created our function (or recipe), we can use it whenever we want.
By reading this article, you've invested 1.03 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.
For more detailed understanding on how to handle events in JavaScript, you can read about Event handler and addEventListener() Also, learn how to Manipulate the DOM to modify the content and structure of the webpage in response to user actions.
About author
Roman Y.
Senior Software Engineer at Nike
Certain articles only distributed to subscribers through email