Checkbox onChange in React
We'll cover how to send a value from a checkbox with a checkbox event in React onChange
.
While developing web applications or business software, we need to use check boxes to display a list of options from which the user can select.
The user can select one to several options from the list of options and is not restricted to selecting only one checkbox, but the user can even select all the checkboxes.
Each checkbox is independent of all the other checkboxes in the list, so checking one box does not uncheck the other checked checkboxes.
This tutorial will create a list of checkboxes and send them to getValue
a function with console.log
their values. Let's create a new application using the following command.
# react
npx create-react-app my-app
After creating our new React application, we will go to the application directory using this command.
# react
cd my-app
Now, let's run our application to check if all dependencies are installed correctly.
# react
npm start
Let's create a list of checkboxes with different values using the method checkValue
that calls the function.onChange
# react
<div className="App">
<h1>Select What You Want</h1>
<div>
<input id="box1" onChange={checkValue} type="checkbox" value="Box1"/>
<label htmlFor="#box1">Box 1</label>
</div>
<div>
<input id="box2" onChange={checkValue} type="checkbox" value="Box2"/>
<label htmlFor="#box2">Box 2</label>
</div>
<div>
<input id="box3" onChange={checkValue} type="checkbox" value="Box3"/>
<label htmlFor="#box3">Box 3</label>
</div>
<div>
<input id="box4" onChange={checkValue} type="checkbox" value="Box4"/>
<label htmlFor="#box4">Box 4</label>
</div>
</div>
Once we have created a list of checkboxes using the method checkValue
of calling function , we will create the function which will accept a parameter containing the value of the checked checkbox and we will get the value of the checkbox checked by the user.onChange
checkValue
e
console.log
# react
function checkValue(e) {
var value = e.target.value;
console.log("You selected " + value);
}
Output:
As shown above, when we check any checkbox, it sets console.log
the value of that checkbox.
There is a problem if we onChange
send the checkbox values using . If we uncheck any one of the checkboxes, the function checkValue
will be called again and we will receive the values of the checkboxes that the user did not check as shown below.
Output:
As shown above, when we uncheck box 1, the value of box 1 is again sent to the function. If we are working with multiple checkboxes and want to save the values of the checkboxes, this would not be the best way to get and save the values.
The best way to get and save the checkbox value is through a form.
However, if we are working with something that works with one checkbox and depends entirely on the state change of that checkbox, then using onChange
the method to perform some functionality might be a good choice.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Using onChange event in React
Publish Date:2025/03/03 Views:148 Category:React
-
onChange 是 React 中最常见的输入事件之一。本文将帮助你了解它的工作原理。
onDoubleClick in React
Publish Date:2025/03/03 Views:128 Category:React
-
This tutorial demonstrates how to use onDoubleClick in React.
Show element or text on hover in React
Publish Date:2025/03/03 Views:186 Category:React
-
To show an element or text on hover in React: Set onMouseOver and onMouseOut properties on the element. Track whether the user is hovering over the element in a state variable. Conditionally render another element based on the state variable. import {
Scroll to top of page in React.js
Publish Date:2025/03/03 Views:182 Category:React
-
In React, use the window.scrollTo() method to scroll to the top of the page, for example, window.scrollTo(0, 0) . The scrollTo method on the window object scrolls to a specific set of coordinates in the document. import {useEffect} from react ; export
Applying global CSS styles in React applications
Publish Date:2025/03/03 Views:145 Category:React
-
要在 React 应用程序中应用全局 CSS 样式,请将 CSS 写入扩展名为 .css 的文件中,并将其导入 index.js 文件中。 全局 CSS 应该在 index.js 中导入,以确保它被加载到你的 React 应用程序的所有
Passing events and parameters to onClick in React
Publish Date:2025/03/03 Views:63 Category:React
-
Passing events and arguments onClick in React: Pass an inline function to the onClick attribute of the element. The function should get the event object and call handleClick. Pass the event and arguments to handleClick. const App = () = { const handle
How to remove event listeners in React
Publish Date:2025/03/03 Views:194 Category:React
-
To remove an event listener in React: Add an event listener in the useEffect hook. Return a function from the useEffect hook. When the component unmounts, remove the event listener using the removeEventListener method. import {useRef, useEffect} from
Using conditions to jump out of a map in map() in React
Publish Date:2025/03/03 Views:147 Category:React
-
Using conditions in map() in React: Call the map() method on an array. Use an if condition to explicitly return if the condition is met. Otherwise return a different value or return null to render nothing. export default function App () { const arr =
Calling multiple onClick functions in React
Publish Date:2025/03/03 Views:105 Category:React
-
To call multiple onClick functions in React: Set the onClick attribute on the element. Call other functions in the event handler. The event handler can call as many other functions as needed. export default function App () { const s