screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script> <script src="https://unpkg.com/@babel/standalone@7.14.7/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState } = React; const App = () => { // Step 1: Create a state variable to store the selected value const [selectedValue, setSelectedValue] = useState(''); // Step 2: Update the state when the user selects an option const handleSelectChange = (event) => { setSelectedValue(event.target.value); }; // Step 3: Handle button click and use the selected value const handleButtonClick = () => { // Do something with the selected value, e.g., display it or pass it to a function console.log('Selected Value:', selectedValue); alert('check in console') }; return ( <div className='container'> <h3>React Js Get Select Value onclick button</h3> {/* Step 2: Attach an onChange event to the select element */} <select onChange={handleSelectChange} value={selectedValue}> <option value="">Select an option</option> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select> {/* Step 3: Attach an onClick event to the button */} <button onClick={handleButtonClick}>Get Selected Value</button> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> /* Styles for the container and button */ .container { max-width: 500px; margin: 0 auto; padding: 20px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); display: flex; flex-direction: column; align-items: center; justify-content: center; } /* Styling for the select element */ .container select { width: 100%; padding: 10px; margin-bottom: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 16px; outline: none; } /* Styling for the option elements */ .container option { font-size: 16px; } button { background-color: #007bff; color: #fff; border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s ease; } /* Hover effect for the button */ button:hover { background-color: #0056b3; } </style> </body> </html>