Form Events
JavaScript form events allow us to respond when users interact with HTML forms and their input controls.
What are Form Events?
Form events are triggered when users interact with HTML forms and their input controls.
Common form-related events include input, change, submit, reset, focus, and blur.
Simple Definition
User Interaction → Form Event → JavaScript Handler → Action
Example of an HTML form containing text fields, selection controls, checkboxes and a submit button. Click the image to open the online reference.
Form Event Flow
Interaction
Input / Select
input / change
Handler
Output
Common Form Events
input occurs when the value of an input field changes.
change occurs when a form control finishes changing its value.
submit occurs when a form is submitted.
focus occurs when an input receives focus.
blur occurs when an input loses focus.
Example 1: Input Event
The input event can be used to detect changes made by a user in a form field.
const event = {
type: "input",
field: "Name",
value: "Rahul"
};
console.log("Event Type:", event.type);
console.log("Field:", event.field);
console.log("Value:", event.value);
Field: Name
Value: Rahul
Example 2: Change Event
The change event is commonly used when the value of a form control changes.
const event = {
type: "change",
field: "Course",
value: "JavaScript"
};
console.log("Event Type:", event.type);
console.log("Field:", event.field);
console.log("Value:", event.value);
Field: Course
Value: JavaScript
Example 3: Submit Event
The submit event occurs when a user submits a form.
const event = {
type: "submit",
form: "Registration Form",
status: "Submitted"
};
console.log("Event Type:", event.type);
console.log("Form:", event.form);
console.log("Status:", event.status);
Form: Registration Form
Status: Submitted
Example 4: Focus and Blur
The focus event occurs when an input becomes active, while blur occurs when the input loses focus.
const focusEvent = {
type: "focus",
field: "Email"
};
const blurEvent = {
type: "blur",
field: "Email"
};
console.log("Focus Event:", focusEvent.type);
console.log("Field:", focusEvent.field);
console.log("Blur Event:", blurEvent.type);
console.log("Field:", blurEvent.field);
Field: Email
Blur Event: blur
Field: Email
Example 🌍🤓
Form events are commonly used in registration, login, search, feedback, and admission forms.
const registration = {
name: "Rahul",
course: "JavaScript",
event: "submit",
status: "Registration Successful"
};
console.log("Student:", registration.name);
console.log("Course:", registration.course);
console.log("Event:", registration.event);
console.log("Status:", registration.status);
Course: JavaScript
Event: submit
Status: Registration Successful
Important Note
Form events such as submit, input, change, focus, and blur normally work with HTML elements inside a browser.
The CIIT JavaScript compiler is console-based, so the Try It examples demonstrate the same event information using JavaScript objects. This prevents the document is not defined error in the compiler.
Summary
JavaScript form events allow web pages to respond to user interaction with forms. Common events include input, change, submit, focus, and blur. These events are useful for validation, user feedback, form processing, and interactive web applications.