MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Handling Events: click, input, submit0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
10.1Selecting Elements: getElementById, querySelector10.2Changing Content and Style10.3Handling Events: click, input, submit
HomeTutorialsJavaScript for BeginnersHandling Events: click, input, submit
Using console.log()

Handling Events: click, input, submit

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Websites become truly alive when they respond to human actions: clicking a button, typing in a text field, moving the mouse, or submitting a form. In JavaScript, these user actions are called Events, and we handle them using Event Listeners.


1. The Standard: addEventListener()

The modern, universally adopted method to listen for events is addEventListener:

1element.addEventListener(eventType, callbackFunction);

Why NOT use onclick = ...?

Using .addEventListener() allows you to attach multiple independent listeners to the same element without overwriting each other, and lets you remove listeners when no longer needed.


2. Common Event Types

A. Mouse Events: click, dblclick, mouseenter, mouseleave

1const card = document.querySelector(".card");
2 
3card.addEventListener("mouseenter", () => {
4 card.classList.add("shadow-lg");
5});
6 
7card.addEventListener("mouseleave", () => {
8 card.classList.remove("shadow-lg");
9});

B. Keyboard & Input Events: input, change, keydown

  • input: Fires immediately on every keystroke as the user types (ideal for live search and instant character counters).
  • change: Fires only when the user finishes and leaves the input field (loses focus).
1const searchInput = document.querySelector("#search");
2const liveCount = document.querySelector("#char-count");
3 
4searchInput.addEventListener("input", (event) => {
5 // Access the current value typed by the user:
6 const currentText = event.target.value;
7 liveCount.textContent = `Characters: ${currentText.length}`;
8});

3. Form Submission: submit and e.preventDefault()

When a user submits an HTML <form>, the browser's default behavior is to reload the entire web page and send a GET/POST HTTP request. In modern Single Page Applications (SPAs), we intercept this using e.preventDefault():

1const loginForm = document.querySelector("#login-form");
2 
3loginForm.addEventListener("submit", (e) => {
4 // 1. STOP the browser from reloading the page!
5 e.preventDefault();
6 
7 // 2. Read input values:
8 const emailInput = document.querySelector("#email").value;
9 const passwordInput = document.querySelector("#password").value;
10 
11 console.log("Submitting login asynchronously for:", emailInput);
12 // Perform API fetch call without any page reload!
13});

4. The Event Object (e or event)

When an event triggers, JavaScript automatically passes an Event Object to the callback function. It contains vital context:

  • e.target: The actual DOM element that triggered the event.
  • e.target.value: The current value of an input element.
  • e.key: The specific key pressed during a keyboard event (e.g., "Enter", "Escape").
  • e.preventDefault(): Prevents default browser action (reloads, navigation).

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the recommended modern method to attach an event handler to an HTML element?
Question 2
What is the critical purpose of calling e.preventDefault() inside a form's submit event listener?
Question 3
Which event fires continuously and immediately on every single keystroke as the user types into an <input> field?
Question 4
Inside an event callback (e) => { ... }, how do you access the current value of the text input that triggered the event?
Question 5
How can you detect if the user pressed the "Enter" key inside a keydown event listener?
Next Lesson

Using console.log()

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Changing Content and StyleUsing console.log()

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext