🧑🏻‍💻

JavaScript Eventing

Image
https://res.cloudinary.com/practicaldev/image/fetch/s--J43C-IwA--/c_imagga_scale,f_auto,fl_progressive,h_900,q_auto,w_1600/https://dev-to-uploads.s3.amazonaws.com/i/1zm80qaekzgu8p54w98g.jpg
JavaScript event handling is used to respond to user interaction with a web page, such as clicks, form submissions, and mouse movements. Events are used to trigger JavaScript code, which can then manipulate the DOM and make changes to the web page.
Here's an example of how to add an event listener to a button that displays an alert message when clicked:
<button id="myButton">Click Me</button> <script> var button = document.getElementById("myButton"); button.addEventListener("click", function(){ alert("Button was clicked"); }); </script>
Here are some common types of events that can be handled in JavaScript:
  • Mouse events: These events include click, dblclick, mousedown, mouseenter, mouseleave, mousemove, mouseover, and mouseup.
  • Keyboard events: These events include keydown, keypress, and keyup.
  • Form events: These events include blur, change, focus, focusin, focusout, input, select, and submit.
  • Document and window events: These events include beforeunload, error, resize, scroll, and unload.
  • Event listeners can also be added using on keyword. For example:
document.getElementById("myButton").onclick = function() { alert("Button was clicked"); };
Event handling in JavaScript can also be used to add dynamic behavior to a web page, such as form validation, updating the page based on user actions, and providing feedback to the user.
It's important to note, you can also stop events from propagating by using the stopPropagation() method and also can prevent default behavior
  • Event Bubbling and Event Capturing: JavaScript events propagate or flow from the innermost element to the outermost element in the DOM tree. This is called event bubbling. If you set up an event listener on the parent element, it will also receive the event from the child element. In contrast, event capturing flows from the outermost element to the innermost element. The event first reaches the outermost element and then propagates to the innermost element. This can be set by passing in useCapture parameter as true while adding event listener.
  • Removing event listeners: You can remove an event listener by using the removeEventListener method, you will have to pass in the same event type, function and useCapture value as passed while adding the event listener.
var button = document.getElementById("myButton"); button.addEventListener("click", myFunction); //... button.removeEventListener("click", myFunction);
  • Event propagation: Events propagate through the DOM tree. When an event is fired, it will first be handled by the element that the event was directly bound to, and then it will propagate up through the parent elements, until it reaches the document object.
  • Event Delegation: you can use event delegation to handle events for multiple elements at once by adding a single event listener to a parent element, instead of adding an event listener to each child element individually.
  • Event object: Every event has an associated event object, it can be accessed via the event parameter that is passed to the event handler function. The event object contains information about the event, such as the type of event, the target element, and the current state of the keyboard and mouse.
  • event.preventDefault(): This method is used to prevent the default action of an event from happening. For example, if you have a link with an href, when you click on the link, it will take you to the specified href, but if you use event.preventDefault() it will stop the link from redirecting.
Event handling and manipulation is a vast topic, and these examples are just the tip of the iceberg, you can learn more by exploring libraries like jQuery, React, Angular and Vue, which provide more convenient and sophisticated ways of handling events.
Built with Potion.so