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 = () => { const [selectedOption, setSelectedOption] = useState(''); const options = [ { key: '1', value: 'option1', label: 'Option 1' }, { key: '2', value: 'option2', label: 'Option 2' }, { key: '3', value: 'option3', label: 'Option 3' }, // Add more options as needed ]; const handleDropdownChange = (event) => { const selectedValue = event.target.value; setSelectedOption(selectedValue); // Find the corresponding object from the options array const selectedOptionObject = options.find((option) => option.value === selectedValue); if (selectedOptionObject) { const selectedOptionKey = selectedOptionObject.key; console.log('Selected option key:', selectedOptionKey); } }; return ( <div className='container'> <h2>React Js get key of selected value from Dropdown</h2> <h3>Selected Option: {selectedOption}</h3> <select onChange={handleDropdownChange}> <option value="">Select an option</option> {options.map((option) => ( <option key={option.key} value={option.value}> {option.label} </option> ))} </select> </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>