Handling Click, Change, and Submit Events
Handling Click, Change, and Submit Events
1. The Core Trio of Web Events
In interactive web engineering, the vast majority of user interactions revolve around three fundamental event types:
- 1
onClick: Triggered when the user clicks or taps an element (buttons, links, badges, cards). - 2
onChange: Triggered when form input values change (text inputs, checkboxes, radio buttons, dropdown selects). - 3
onSubmit: Triggered when a<form>element is submitted (via Enter key or submit button click).
Mastering how React harmonizes these three events unlocks complete control over client-side interactivity.
2. Handling Form Inputs with onChange
In standard HTML, the change event often only fires when an input loses focus (the blur event). In React, onChange fires immediately on every single keystroke or value modification.
This real-time response makes it effortless to implement Controlled Components, where input values are strictly bound to React state:
Key Detail: For checkboxes, read e.target.checked (boolean), NOT e.target.value (string).
3. Handling Form Submissions with onSubmit
Submitting a <form> in standard HTML causes the browser to reload the entire web page and submit a GET or POST request to the action URL. In a React Single-Page Application, you intercept this behavior using e.preventDefault() inside your onSubmit handler:
Practice Quiz
onChange event fire for a text input?<input type="checkbox" />), which property of e.target holds its checked status?onSubmit to the <form> element preferred over attaching onClick to the submit button?onSubmit handler to prevent the browser from reloading the page?Preventing Default Browser Behaviors
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Passing Arguments to Event Handlers | Preventing Default Browser Behaviors |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.