screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script> <script src="https://cdn.jsdelivr.net/npm/@babel/standalone@7.14.6/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel" data-type="module"> const { useState } = React; const App = () => { const [checkboxValues, setCheckboxValues] = useState([]); const handleCheckboxChange = (event) => { const { value, checked } = event.target; if (checked) { setCheckboxValues((prevValues) => [...prevValues, value]); } else { setCheckboxValues((prevValues) => prevValues.filter((item) => item !== value) ); } }; return ( <div className="container"> <h3>React checkbox if checked add value to array</h3> <label className="vegetable-item"> <input type="checkbox" value="Carrot" checked={checkboxValues.includes("Carrot")} onChange={handleCheckboxChange} /> Carrot </label> <label className="vegetable-item"> <input type="checkbox" value="Broccoli" checked={checkboxValues.includes("Broccoli")} onChange={handleCheckboxChange} /> Broccoli </label> <label className="vegetable-item"> <input type="checkbox" value="Spinach" checked={checkboxValues.includes("Spinach")} onChange={handleCheckboxChange} /> Spinach </label> <label className="vegetable-item"> <input type="checkbox" value="Tomato" checked={checkboxValues.includes("Tomato")} onChange={handleCheckboxChange} /> Tomato </label> <label className="vegetable-item"> <input type="checkbox" value="Cucumber" checked={checkboxValues.includes("Cucumber")} onChange={handleCheckboxChange} /> Cucumber </label> <label className="vegetable-item"> <input type="checkbox" value="Bell Pepper" checked={checkboxValues.includes("Bell Pepper")} onChange={handleCheckboxChange} /> Bell Pepper </label> {/* Display the selected values */} <p>Selected Vegetables: {checkboxValues.join(", ")}</p> </div> ); }; ReactDOM.render(<App />, document.getElementById("app")); </script> <style> body { margin: 0; } .container { max-width: 800px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); position: relative; } /* Style for the vegetable items */ .vegetable-item { display: block; margin-bottom: 10px; position: relative; padding-left: 30px; cursor: pointer; font-size: 16px; user-select: none; } /* Style for the selected vegetables text */ p { margin-top: 20px; font-size: 18px; font-weight: bold; } </style> </body> </html>