[html] How can I make an entire HTML form "readonly"?

I have two pages with HTML forms. The first page has a submission form, and the second page has an acknowledgement form. The first form offers a choice of many controls, while the second page displays the data from the submission form again with a confirmation message. On this second form all fields must be static.

From what I can see, some form controls can be readonly and all can be disabled, the difference being that you can still tab to a readonly field.

Rather than doing this field by field is there any way to mark the whole form as readonly/disabled/static such that the user can't alter any of the controls?

This question is related to html

The answer is


Wrap the input fields and other stuff into a <fieldset> and give it the disabled="disabled" attribute.

Example (http://jsfiddle.net/7qGHN/):

_x000D_
_x000D_
<form>_x000D_
    <fieldset disabled="disabled">_x000D_
        <input type="text" name="something" placeholder="enter some text" />_x000D_
        <select>_x000D_
            <option value="0" disabled="disabled" selected="selected">select somethihng</option>_x000D_
            <option value="1">woot</option>_x000D_
            <option value="2">is</option>_x000D_
            <option value="3">this</option>_x000D_
        </select>_x000D_
    </fieldset>_x000D_
</form>
_x000D_
_x000D_
_x000D_


Not all form elements can be set to readonly, for example:

  • checkboxes
  • radio boxes
  • file upload
  • ...more..

Then the reasonable solution would be to set all form elements' disabled attributes to true, since the OP did not state that the specific "locked" form should be sent to the server (which the disabled attribute does not allow).

Another solution, which is presented in the demo below, is to place a layer on top of the form element which will prevent any interaction with all the elements inside the form element, since that layer is set with a greater z-index value:

DEMO:

_x000D_
_x000D_
var form = document.forms[0], // form element to be "readonly"_x000D_
    btn1 = document.querySelectorAll('button')[0],_x000D_
    btn2 = document.querySelectorAll('button')[1]_x000D_
_x000D_
btn1.addEventListener('click', lockForm)_x000D_
btn2.addEventListener('click', lockFormByCSS)_x000D_
_x000D_
function lockForm(){_x000D_
  btn1.classList.toggle('on');_x000D_
  [].slice.call( form.elements ).forEach(function(item){_x000D_
      item.disabled = !item.disabled;_x000D_
  });_x000D_
}_x000D_
_x000D_
function lockFormByCSS(){_x000D_
  btn2.classList.toggle('on');_x000D_
  form.classList.toggle('lock');_x000D_
}
_x000D_
form{ position:relative; } _x000D_
form.lock::before{_x000D_
  content:'';_x000D_
  position:absolute;_x000D_
  z-index:999;_x000D_
  top:0;_x000D_
  right:0;_x000D_
  bottom:0;_x000D_
  left:0;_x000D_
}_x000D_
_x000D_
button.on{ color:red; }
_x000D_
<button type='button'>Lock / Unlock Form</button>_x000D_
<button type='button'>Lock / Unlock Form (with CSS)</button>_x000D_
<br><br>_x000D_
<form>_x000D_
  <fieldset>_x000D_
    <legend>Some Form</legend>_x000D_
    <input placeholder='text input'>_x000D_
    <br><br>_x000D_
    <input type='file'>_x000D_
    <br><br>_x000D_
    <textarea placeholder='textarea'></textarea>_x000D_
    <br><br>_x000D_
    <label><input type='checkbox'>Checkbox</label>_x000D_
    <br><br>_x000D_
    <label><input type='radio' name='r'>option 1</label>_x000D_
    <label><input type='radio' name='r' checked>option 2</label>_x000D_
    <label><input type='radio' name='r'>option 3</label>_x000D_
    <br><br>_x000D_
    <select>_x000D_
      <option>options 1</option>_x000D_
      <option>options 2</option>_x000D_
      <option selected>options 3</option>_x000D_
    </select>_x000D_
  </fieldset>_x000D_
</form>
_x000D_
_x000D_
_x000D_


There is no built-in way that I know of to do this so you will need to come up with a custom solution depending on how complicated your form is. You should read this post:

Convert HTML forms to read-only (Update: broken post link, archived link)

EDIT: Based on your update, why are you so worried about having it read-only? You can do it via client-side but if not you will have to add the required tag to each control or convert the data and display it as raw text with no controls. If you are trying to make it read-only so that the next post will be unmodified then you have a problem because anyone can mess with the post to produce whatever they want so when you do in fact finally receive the data you better be checking it again to make sure it is valid.


You add html invisible layer over the form. For instance

<div class="coverContainer">
<form></form>
</div>

and style:

.coverContainer{
    width: 100%;
    height: 100%;
    z-index: 100;
    background: rgba(0,0,0,0);
    position: absolute;
}

Ofcourse user can hide this layer in web browser.


You can use this function to disable the form:

function disableForm(formID){
  $('#' + formID).children(':input').attr('disabled', 'disabled');
}

See the working demo here

Note that it uses jQuery.


I'd rather use jQuery:

$('#'+formID).find(':input').attr('disabled', 'disabled');

find() would go much deeper till nth nested child than children(), which looks for immediate children only.


Easiest way

$('#yourform *').prop('readonly', true);

This is an ideal solution for disabling all inputs, textareas, selects and buttons in a specified element.

For jQuery 1.6 and above:

// To fully disable elements
$('#myForm :input').prop('disabled', true); 

Or

// To make elements readonly
$('#myForm :input').prop('readonly', true); 

jQuery 1.5 and below:

$('#myForm :input').prop('disabled', 'disabled');

And

$('#myForm :input').prop('readonly', 'readonly');

Another simple way that's supported by all browsers would be:

HTML:

<form class="disabled">
  <input type="text" name="name" />
  <input type="radio" name="gender" value="male">
  <input type="radio" name="gender" value="female">
  <input type="checkbox" name="vegetarian">
</form>

CSS:

.disabled {
  pointer-events: none;
  opacity: .4;
}

But be aware, that the tabbing still works with this approach and the elements with focus can still be manipulated by the user.


On the confirmation page, don't put the content in editable controls, just write them to the page.


Have all the form id's numbered and run a for loop in JS.

 for(id = 0; id<NUM_ELEMENTS; id++)
   document.getElementById(id).disabled = false; 

There's no fully compliant, official HTML way to do it, but a little javascript can go a long way. Another problem you'll run into is that disabled fields don't show up in the POST data