. When isDisabled is true, the input will be disabled; when false, it will be enabled. ">
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.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.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 { useState } = React; function App() { const [isFieldDisabled, setIsFieldDisabled] = useState(false); const handleCheckboxChange = (event) => { setIsFieldDisabled(event.target.checked); }; return ( <div className='container'> <h3>React Js Disable Input Field Based on Condition</h3> <label> Disable Input Field: <input type="checkbox" onChange={handleCheckboxChange} /> </label> <br /> <input type="text" placeholder="Type here..." disabled={isFieldDisabled} /> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> .container { max-width: 400px; margin: 0 auto; padding: 20px; border: 1px solid #ccc; border-radius: 8px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); background-color: #ffffff; } h3 { font-size: 24px; margin-bottom: 20px; color: #333; } label { display: block; font-size: 18px; color: #666; margin-bottom: 10px; } input[type="checkbox"] { margin-right: 10px; } input[type="text"] { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 6px; font-size: 16px; color: #333; box-sizing: border-box; transition: border-color 0.3s ease-in-out; } </style> </body> </html>