Open In App

Explain the purpose of the onChange event in form handling.

Forms play a crucial role in web applications, allowing users to input data, submit information, and interact with the website. To enhance this interaction, the onChange event becomes a key player in form handling within the realm of web development.

In this article, we’ll explore the purpose and significance of the onChange event and its role in creating dynamic and responsive forms.



Prerequisites:

Why Do We Need Form Handling?

Forms act as a bridge between users and applications, providing a means for users to input data that applications can process. Form handling, therefore, involves the management of user input, validation, and the dynamic updating of the user interface based on the entered data. This is where the onChange event steps in.



Key Aspects and Applications:

Approach to implement onChange Event:

Steps To Create React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app newapp

Step 2: After creating your project folder(i.e. newapp), move to it by using the following command:

cd newapp

Step 3. After setting up react environment on your system, we can start creating our form in App.js file.

Project Structure:

The dependencies in the package.json file.

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},

Example: Below the example to handle form:




// App.js
 
import React, { useState } from "react";
import "./App.css";
 
const App = () => {
    const [formData, setFormData] = useState({
        name: "",
        email: "",
        message: "",
    });
 
    const handleChange = (e) => {
        const { name, value } = e.target;
        setFormData({
            ...formData,
            [name]: value,
        });
    };
 
    const handleSubmit = (e) => {
        e.preventDefault();
        console.log("Form submitted:", formData);
    };
 
    return (
        <div className="green-theme">
            <form onSubmit={handleSubmit}>
                <label htmlFor="name">Name:</label>
                <input
                    type="text"
                    id="name"
                    name="name"
                    value={formData.name}
                    onChange={handleChange}
                />
 
                <label htmlFor="email">Email Address:</label>
                <input
                    type="email"
                    id="email"
                    name="email"
                    value={formData.email}
                    onChange={handleChange}
                />
 
                <label htmlFor="message">Message:</label>
                <textarea
                    id="message"
                    name="message"
                    value={formData.message}
                    onChange={handleChange}
                />
 
                <button type="submit">Submit</button>
            </form>
        </div>
    );
};
 
export default App;




.App {
    text-align: center;
}
 
.green-theme {
    background-color: #4caf50;
    padding: 20px;
    border-radius: 8px;
    width: 300px;
    margin: auto;
}
 
form {
    display: flex;
    flex-direction: column;
}
 
label {
    margin-bottom: 8px;
    color: #ffffff;
}
 
input,
textarea {
    padding: 8px;
    margin-bottom: 16px;
    border: 1px solid #ffffff;
    border-radius: 4px;
}
 
button {
    background-color: #ffffff;
    color: #4caf50;
    padding: 10px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}
 
button:hover {
    background-color: #e7e7e7;
}

Start your application using the following command:

npm start

Output:


Article Tags :