screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useRef } = React; function App() { const checkboxesRef = useRef([]); const checkboxValue = (e) => { console.log(e.target.value); }; const uncheckAll = () => { checkboxesRef.current.forEach((checkbox) => { checkbox.checked = false; }); }; const checkAll = () => { checkboxesRef.current.forEach((checkbox) => { checkbox.checked = true; }); }; return ( <div className='container'> <h3>React Js check/uncheck all checkboxes with a button</h3> <label> <input ref={(element) => { checkboxesRef.current.push(element); }} value='Facebook' type='checkbox' onChange={checkboxValue} /> Facebook </label> <br /> <label> <input ref={(element) => { checkboxesRef.current.push(element); }} value='Apple' type='checkbox' onChange={checkboxValue} /> Apple </label> <br /> <label> <input ref={(element) => { checkboxesRef.current.push(element); }} value='Google' type='checkbox' onChange={checkboxValue} /> Google </label> <br /> <label> <input ref={(element) => { checkboxesRef.current.push(element); }} value='Chatgpt' type='checkbox' onChange={checkboxValue} /> Chatgpt </label> <br /> <div className='buttonGroup'> <button ClassName='uncheckAll' onClick={uncheckAll}>Unchecked</button> <button className='checkAll' onClick={checkAll}>Checked</button> </div> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } body { font-family: Arial, sans-serif; margin: 0; padding: 0; } .container { margin: 0 auto; width: 600px; padding: 20px; display: flex; align-items: center; flex-direction: column; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); } label { display: flex; align-items: center; margin-bottom: 10px; } input[type="checkbox"] { margin-right: 10px; } button { padding: 10px 20px; margin-top: 10px; font-size: 16px; border: none; border-radius: 4px; cursor: pointer; } .uncheckAll { background-color: #FF6464; color: white; } .checkAll { background-color: #42B983; color: white; margin-left: 1rem; } </style> </body> </html>