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.

<HTML>
<HEAD>

<script>
	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;
				}
			}
		}
	}
</script>

</HEAD>

<BODY>

	<form method="post" action="http://www.sdfsdf.com" onSubmit="return disableForm(this);">
		<input type="text" name="Text1">
		<input type="submit" name="SubmitButton" value="Submit">
	</form>
	
</BODY>
</HTML>

You might also like...

Comments

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

Contribute

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.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler