View Full Version : Disable all form elements

Gary Williams
03-02-2003, 07:30 PM
Hi All,

I have a form that is populated from a database. To prevent the data from being resubmitted, I want to disable every single form element so that the complete form is read only. If any of the data needs editing and resubmitting, an edit button needs to be clicked on to enable all form elements for editing.

Does anyone have a script that will cycle through all form element types (irrespective of their 'names', 'id's', etc) to disable/enable enmasse?


03-02-2003, 08:16 PM

for(x=0;x<len;x++) {

Gary Williams
03-24-2003, 08:30 AM
Hi Chris,

Thanks for the code you sent, it works a treat. I triggered it via an on-load and called it as a function from a button. All worked.

I then tried to enable the form elements by calling a function from outside the form (but on the same page), as follows:

for(x=0;x<len;x++) {
document.formname.elements[x].disabled=false ;

This did not work. What am I failing to understand about this script?



03-24-2003, 08:48 AM
shouldn't it be:


and to make a single function to enable/disable elements:

function enableDisable(flag){
//flag = true if disabled, false if enabled
var f = document.formname;
var len=f.elements.length;
for(x=0;x<len;x++) {

Gary Williams
03-24-2003, 09:17 AM
Hi Glenn,

Thanks, I'll try it out now.

How do you guys do this so fast? It takes me hours sorting out these things.



03-24-2003, 09:00 PM
Using elements is more proper.

I'd do it like this anyway
for( var elem, i = 0; ( elem = document.formname.elements[i] ); i++ )
elem.disabled = false ;

02-04-2005, 07:43 PM
I want to have an HTML form that depending on which radio button the user selects, different sets of form inputs are enabled. That is, one set of inputs (checkboxes and text input fields) are enabled when option 1 is selected, a second set is enabled when option 2 is selected, etc.

I searched the web and forums for this, but all I found was ways to disable/enable the entire form, not a subset of all the elements on the form. I think that the FIELDSET property could help me, but there is no LENGTH attribute for it. Any suggestions?


Puffin the Erb
02-04-2005, 09:38 PM
What if the visitor has disabled JavaScript or is using a non-scripting browser?
They will be able to submit data maliciously / accidentally.

Make sure this will not be a mission-critical problem - if so carry out further validation server-side.