- Event handlers are JavaScript functions that respond to events, making web pages interactive.
- It’s similar to a doorbell ringing when someone's at the door.
- Click Events: Clicking a button with the mouse.
- Key Press Events: Typing on a keyboard.
- Mouse Over Events: Hovering over an element.
- Form Submission Events: Submitting a form.
.addEventListener
is the preferred way for adding event listeners in JS. It keeps JS separate from HTML for cleaner code.
- Allows you to add multiple listeners to a single event.
element.addEventListener('click', function() {
alert('Hello, World!');
});