screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.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; function App() { const [isVisible, setIsVisible] = useState(true); const handleToggleVisibility = () => { // Access the DOM element by its ID const elementById = document.getElementById('myElementId'); if (elementById) { // Toggle the visibility elementById.style.display = isVisible ? 'none' : 'block'; setIsVisible(!isVisible); } else { console.error("Element with ID 'myElementId' not found."); } }; return ( <div className='container'> <h3>React Js Get Element by id | Toggle Visibility </h3> <button onClick={handleToggleVisibility}>Toggle Visibility</button> <div id="myElementId" style={{ display: isVisible ? 'block' : 'none' }}> This is my element </div> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { text-align: center; margin: 50px auto; padding: 20px; width: 300px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); } h3 { color: #333; } button { background-color: #007bff; color: #fff; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #0056b3; } #myElementId { margin-top: 20px; padding: 10px; background-color: #fff; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); } </style> </body> </html>