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 { useState } = React; function App() { const [inputValues, setInputValues] = useState(['fontawesomeicons.com','tutorialsplane.com']); const handleInputChange = (index, value) => { setInputValues((prevInputValues) => { const updatedInputValues = [...prevInputValues]; updatedInputValues[index] = value; return updatedInputValues; }); }; const handleGetValue = (index) => { alert(inputValues[index]); // You can do whatever you want with the value here }; const handleAddInput = () => { setInputValues((prevInputValues) => [...prevInputValues, '']); }; const handleRemoveInput = (index) => { setInputValues((prevInputValues) => { const updatedInputValues = [...prevInputValues]; updatedInputValues.splice(index, 1); return updatedInputValues; }); }; return ( <div className='container'> <h3>React Js Get and add multiple Input field value dynamically</h3> {inputValues.map((value, index) => ( <div key={index}> <input type="text" value={value} onChange={(event) => handleInputChange(index, event.target.value)} /> <button onClick={() => handleGetValue(index)}>Get Value</button> <button onClick={() => handleRemoveInput(index)}>Remove</button> </div> ))} <button onClick={handleAddInput}>Add Input</button> </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); } .container>div { margin-bottom: 10px; } input[type="text"] { padding: 5px; border: 1px solid #ccc; border-radius: 3px; width: 200px; } button { padding: 5px 10px; margin-left: 5px; border: none; border-radius: 3px; background-color: #007bff; color: #fff; cursor: pointer; } button:hover { background-color: #0056b3; } </style> </body> </html>