How to disable all elements on a form

You can have your form to be disabled as soon as the user pressed the submit button so that he doesn't accidently submit the form twice. This is the code you can use.


	function disableForm(theform) {
		if (document.all || document.getElementById) {
			for (i = 0; i < theform.length; i++) {
			var formElement = theform.elements[i];
				if (true) {
					formElement.disabled = true;



	<form method="post" action="" onSubmit="return disableForm(this);">
		<input type="text" name="Text1">
		<input type="submit" name="SubmitButton" value="Submit">

You might also like...


Edward Tanguay Edward Tanguay updates his personal web site weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth