Deactivate a form when the page loads and activate it upon clicking a button

I have a form that I want to initially disable when the page loads. I am looking to enable it only after clicking a specific button. The technologies I'm using for this are HTML and Angular.

<form name="form" id="form">
 <input type="text">
</form>
 <button type="button">Enable Form</button>

Answer №1

To disable all HTML form elements, simply add the attribute disabled.

Then, when a button is clicked, you can enable them.

function initialize(){
  var form = document.getElementById("myform");
  
  form.forEach(function(element){
    console.log(form.elements[0].value);
  });
}
<body onload="initialize()">
<form id="myform" name="myform">
<input type="text" id="myText" value="123">
<input type="checkbox" id="check">
<input type="radio" id="radio">
<select disabled id="select"><option></option></select>

</form>
<button onclick="myFunction()" type="button">Enable Form</button>
<body>

Type 2: Utilizing jQuery

 function myFunction(){
  $("#form :input").prop("disabled", false);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form" name="form">
<input type="text" id="myText" disabled>
<input type="checkbox" id="check" disabled>
<input type="radio" id="radio" disabled>
<select disabled id="select"><option></option></select>

</form>
<button onclick="myFunction()" type="button">Enable Form</button>

Answer №2

Complete the steps below

<form name="myForm" id="myForm" style='display:none'>
......
</form>
<button type="button" onclick="displayForm()">Show Form</button>
<script> 
       function displayForm() 
            {           
             document.getElementById('myForm').style.display="block";
            }
</script>

Answer №3

Are you in need of this information? https://example.com/code-sample

<form name="myForm" id="myForm">
<input type="text" />
<br><br>
<select name="s1">
<option value="">Select</option>
<option value="a">A</option>
<option value="b">B</option>
</select>
<br><br>
<input type="radio" name="radio" value="yes">Yes
<input type="radio" name="radio" value="no">No    

Activate Form

function deactivateForm(){
 var form = document.getElementById("myForm");
 var elements = form.elements;
 for (var i = 0, len = elements.length; i < len; ++i) {
  elements[i].disabled = true;
 }
}
deactivateForm();
document.getElementById("enableForm").addEventListener("click", activateFrm);

function activateFrm(){
 var form = document.getElementById("myForm");
 var elements = form.elements;
for (var i = 0, len = elements.length; i < len; ++i) {
 elements[i].disabled = false;
}
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The OnClick event is unresponsive when accessing the website on a mobile browser

I have a HTML page where I've included an onclick event in a div tag. Within this event, I'm using location.href = url to open a specific URL. Surprisingly, this works perfectly fine in a web browser but strangely doesn't work in a mobile br ...

exploring various dynamic elements using jquery

If you need some help figuring this out, take a look at the JSFiddle here. I've set it up for users to input any data they want in the text box, choose a table from set one, and then click the "submit" button to send it. <div> <Div> < ...

Is there a way to redirect the user to a different page without refreshing the page during navigation?

Utilizing javascript, ajax, and jquery to dynamically load content from PHP files without refreshing the page has been a successful venture. However, I am facing a challenge in creating a hyperlink within one of the loaded contents that redirects the user ...

Is there a way to reference an image in the ROOT/app/assets/images directory prior to precompilation?

My current challenge involves using HTML to display an image before the asset precompilation process, which usually moves all images into the public folder. Whenever I use this code: <img src="Flower.jpg">, it points to the path: <RAILS ROOT>/ ...

The divs descend one after another with no problems related to margins or padding

Having some trouble creating a unique WordPress theme with Bootstrap3. I can't seem to get my divs to behave properly and I've tried everything from display: inline, vertical-align: top, floats, clears, etc... but nothing seems to work. Here is ...

Even after setting a new value to a variable in Vue, it may still reference the old

init(){ this.unsortedList = this.selectedVoucher.approvalStepList; // list in original order this.sortedList = this.unsortedList .sort(function(a,b){ if (new Date(a.createDate) < new Date(b.createDate)) return -1; ...

Constantly positioning the text cursor over the Textbox

I am currently in the process of developing a webpage that will feature only one text box for displaying information based on the input data provided. Our strategy involves utilizing either a Barcode Scanner or Magnetic Swipe as well as a Touch Screen Moni ...

choosing from dropdown menu items

Learn HTML <table> <tr> <td>ENTER PRINCIPLE AMOUNT</td> <td><input type="text" name="principle" size="7"></td> </tr> <tr> <td>ENTER RATE OF INTEREST</td> <td><input type="text" na ...

Concealing elements in Vuestic VueJs for smaller devices

Is there a way to hide an element on small or extra-small screens using the vuestic vuejs framework? <div class="flex md4 sm4 xs0"></div> <div class="flex v-line xs0 sm0 md1"></div> <div class="flex md8 xs12"></div> I ...

Having trouble running the command "npm start"?

Today I started working with electron and followed a tutorial to create a basic browser window application. However, when I ran the command 'npm start' in the terminal, I encountered multiple errors. Below is the console error message along with ...

When an HTML element extends beyond the screen's right boundary, it becomes truncated

My website utilizes a table to showcase some data, but there's an issue when viewed on smaller screens. The table gets cut off and not all the content is visible. Even after scrolling completely to the right, the rightmost field remains barely visible ...

Insert the <span> element within the <a> element with the help of Jquery

I am attempting to insert a span tag inside .block-leftnav ul li .parent a, however, instead of adding the tag after only the .parent a, jQuery is adding the span tag after every href tag on my page. Below is the code I am using to achieve my desired outc ...

Troubleshoot: Dropdown menu in Materialize not functioning (menu fails to drop down

I am currently incorporating Materialize to create a dropdown button in my navigation bar. However, I am facing an issue where nothing happens when the button is clicked. Here is a snippet of my code: <head> <meta charset="UTF-8"> <!-- ...

Adjusting the spacing between rows in Bootstrap 5

Having trouble with the spacing in my latest markup update. Here's what I have: <div class="row"> <div class="col-md-12"> <div class="form-group"> <label class="control-la ...

Rendering HTML in special characters with AngularJS is an innovative way to display

My last question may have been unclear, but I received a similar response from the server asking for clarification. The HTML is not rendering properly here. How can this be resolved? AppControllers.controller('create', ['$scope',' ...

Center the text vertically within a card using Vuetify styling

I am seeking a way to vertically align text within a card using Vuetify or traditional CSS in a Vue project. Here is my code: <template> <div> <v-container class="my-5"> <v-row justify="space-between"> <v- ...

What is the best way to pass the value from one textfield to another using material UI in a react application?

I'm looking to transfer the content from a text field in a dialog window to another text field that is not embedded. However, instead of transferring the actual text field value, I'm getting an output of "object Object". Can you help me figure ou ...

The flexbox layout is not properly stacking div rows in a column

Objective: Creating a flexbox layout with a responsive background image that adjusts in height when the screen size changes, causing elements to wrap; In addition, there is a fixed header bar at the top of the page. The layout consists of a full-screen co ...

Refreshing React state

My basic component subscribes to an EventEmitters event to accumulate an array of messages. However, I'm facing an issue where the message array only ends up containing the latest message. Below is the source code for the component: export const Mess ...

You are only able to click the button once per day

I am working on a button that contains numeric values and updates a total number displayed on the page when clicked. I would like this button to only be clickable once per day, so that users cannot click it multiple times within a 24 hour period. Below i ...