The interaction between JavaScript and HTML is handled via events that occur when the user or the browser manipulates a page. An event occurs when a webpage loads. When a user hits a button, it triggers an event. Other instances include pressing any key, closing or resizing a window, and so on.
Js reacts to these events when javascript code is embedded in HTML and allows execution. Event Handling refers to the process of reacting to occurrences. As a result, JavaScript uses Event Handlers to handle HTML events.
Event handlers are the JavaScript code that invokes a specific piece of code when a particular action happens on an HTML element. The event handler can either invoke the direct JavaScript code or a function.
Only when an event occurs in the context of the object on which they are registered are event listeners called. The button node is given a handler in this example. Clicking the button activates the handler, but clicking anywhere else in the document does not. Adding a onclick attribute to a node has a similar effect.
Using addEventListener, add an event handler to each of the div tags ().
Double-check that the event is "click" here.
The method addEventListener() takes three parameters.
Share a personalized message with your friends.