0

Control flow

How can I use java script to create a button which when clicked a form appears

1st Mar 2025, 6:08 PM
Frederick Appiah
Frederick Appiah - avatar
3 ответов
+ 6
So, your question is about how to use JavaScript to show and hide a form when a button is clicked. First, the HTML structure: there's a button and a form. The form is initially hidden using CSS with display: none. (Simple) The button has onclick="toggleForm()" to trigger the action. So, the main interaction is through that JavaScript function. Now, the JavaScript's role is to toggle the form's visibility. Finds the hidden form using getElementById() Checks if form is visible/hidden Switches between display: block (show) and display: none (hide) And last, updates button text to match current state. Click - JavaScript - DOM Update - Visible Change https://sololearn.com/compiler-playground/WOD76tKi410T/?ref=app
1st Mar 2025, 9:35 PM
✧GHOST✧
✧GHOST✧ - avatar
+ 5
Upgraded Code ( Bob_Li ) <style>.myForm { display: flex; /* Flex layout */ flex-direction: column; } .hidden { display: none; /* Reusable class */ }</style> <button onclick="toggleForm()">Show Form</button> <form id="myForm" class="myForm hidden"> <!-- Uses classes --> <script>/* Toggles .hidden class */</script>
2nd Mar 2025, 5:05 AM
✧GHOST✧
✧GHOST✧ - avatar
+ 1
maybe I don't understand your question. it's not really clear. you need a button on a web page, and when it is clicked, there a form will appear. where and how? if you think, something like a bootstrap modal, then your question is already answered
1st Mar 2025, 6:50 PM
Mihaly Nyilas
Mihaly Nyilas - avatar