High-level programming language.
Event handling is a fundamental aspect of JavaScript and web development. Events are actions or occurrences that happen in the system you are programming, which the system tells you about so you can respond to them in some way if desired. In the context of the web and JavaScript, events are typically user interactions such as clicking a button, scrolling the page, or pressing a key on the keyboard.
There are numerous types of events that can be detected in JavaScript. Some of the most common include:
click
: Fires when a mouse click is detected.load
: Fires when a page or an image has finished loading.mouseover
: Fires when the mouse pointer moves over an element.mouseout
: Fires when the mouse pointer moves out of an element.keydown
: Fires when a key is pressed down.keyup
: Fires when a key is released.Event listeners are functions that wait for an event to occur. They can be added to any DOM object, not only HTML elements. Here is an example of adding an event listener to a button:
let button = document.querySelector('button'); button.addEventListener('click', function() { alert('Button was clicked!'); });
In this example, the addEventListener
method is used to attach a click
event to the button. When the button is clicked, the function is executed, and an alert box appears.
Event listeners can also be removed using the removeEventListener
method. This requires that the function passed in addEventListener
has a name:
let button = document.querySelector('button'); function showAlert() { alert('Button was clicked!'); } button.addEventListener('click', showAlert); button.removeEventListener('click', showAlert);
Event propagation is the process by which an event triggers its listeners, first on the window, then on descendant nodes, and finally on the target node itself. There are two phases of event propagation: capturing and bubbling.
In the capturing phase, the event goes down the DOM tree, from the window
to the target element. In the bubbling phase, the event bubbles up from the target element to the window
.
By default, event handlers are executed in the bubbling phase. However, you can set an event handler to execute in the capturing phase by setting the third argument of addEventListener
to true
.
When an event is fired, information about the event is passed to the event handler function in the form of an event object. This object contains properties and methods related to the event. For example, the target
property can be used to get the element that triggered the event:
button.addEventListener('click', function(event) { console.log(event.target); });
Some events have a default action associated with them. For example, clicking a link navigates to a new URL, and submitting a form sends the form data to the server. You can prevent these default actions using the preventDefault
method of the event object:
let link = document.querySelector('a'); link.addEventListener('click', function(event) { event.preventDefault(); console.log('Link was clicked, but no navigation occurred'); });
In this example, clicking the link will not navigate to a new URL, but instead will log a message to the console.