DOM & Validation

JavaScript Events An Easy Learner

What is an Event?

An event is an action or occurrence detected by the browser, such as a click, keypress, mouse movement, or page load, that JavaScript can respond to.

Common Events

  • click — fires when an element is clicked.
  • mouseover / mouseout — fires when the mouse enters/leaves an element.
  • keydown / keyup — fires when a key is pressed/released.
  • submit — fires when a form is submitted.
  • load — fires when the page finishes loading.

Handling Events

document.getElementById("btn").addEventListener("click", function() {
    alert("Button clicked!");
});

Using addEventListener() is the recommended approach, since it allows attaching multiple handlers to the same event without overwriting existing ones.

Ready to master real-world JavaScript development?

Learn JavaScript hands-on with mentor-led, live sessions.

Explore Course