Library code snippets

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>

Comments

  1. 09 Jun 2008 at 07:00
    Thanks

     

     

  2. 14 May 2007 at 17:54
    I would love to know what the
    "if (true) "

    line is testing and where is this documented?

    Many thanks
    Richard







  3. 01 Jan 1999 at 00:00

    This thread is for discussions of How to disable all elements on a form.

Leave a comment

Sign in or Join us (it's free).

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.

Related podcasts

  • jQuery in ASP.NET

    In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, Scott Koon, and Steven Harman discuss Microsoft's jQuery in ASP.NET announcement.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are you loo...

Events coming up

  • Dec 8

    December Silicon Valley Ruby Meetup

    Moffett Field, United States

    In a World of Middleware, Who Needs Monolithic Applications? by Jon Crosby With Rack emerging as the standard for composing web applications and services, most recently with Rails adoption, an architectural shift is taking place. Learn how to create next generation web services by reusing existing Rack middleware and supplementing with your own components and micro-frameworks like Sinatra. Bio : Jon likes music, the Open Web, Ruby, Erlang, Haskell, Objective-C, JavaScript and coffee.

We'd love to hear what you think! Submit ideas or give us feedback