Disable All Form Elements
Another question I have seen a lot lately is how can I disable all of the form elements on the page with JavaScript? Well it is rather simple and only takes a few lines of code. All you need to do is loop through the Form elemnt array and set the disabled property to true to disable the elements and false to enable the elements.
function DisableEnableForm(xForm,xHow){
objElems = xForm.elements;
for(i=0;i<objElems.length;i++){
objElems[i].disabled = xHow;
}
}
To execute the function, you just need to send the form object reference and a boolean value to the function. The following code is an example how to call the function disable the elements as the page is loading.
window.onload= function(){
DisableEnableForm(document.Form1,true);
}
Hope this helps!
Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages
TrackBacks[0]
Comments[12]
Posted by pascarello on May 17, 2005 9:32:47 AM EST
Reply |
Permalink
Re: Disable All Form Elements
it is helpful
Comment from sreeju on July 17, 2005 12:22:16 AM EST
This is a sweet bit of code. Thanks for posting it, helped allot!
Comment from Feelix on December 21, 2006 8:24:06 AM EST
Eric, just wanted to say thanks for this and the other stuff in your blog. I've been working on a huge project with MySQL, PHP, and using JavaScript for a little spice. More than once you've helped me do the things I need to do (my bookmarks prove it). This tip is exactly what I was looking for. Thanks again!
Comment from Woody on October 18, 2005 4:49:39 PM EST
This is wonderful.
Comment from dts on May 5, 2006 2:08:47 PM EST
Thank You for this little script. Just what I looked for :D
Comment from Gustav on May 12, 2006 1:19:58 PM EST
Thank you!!
Comment from Anonymous on May 16, 2006 3:16:08 PM EST
Thank you buddy. You made life easy.
Comment from ma_ve_ra_swa on September 25, 2006 4:32:44 PM EST
GREAT found after hour of googleing!
Comment from Anonymous on October 5, 2006 7:17:49 AM EST
Thank You So Much. You made it simple and easy.
Comment from Lenin Pinto on October 24, 2006 1:43:16 AM EST
I guess the script will work but if there are a lot of elements..lets say 1000 then it will take a long time to loop through them and disable them..
THanks and Regards,
Aqeel
Comment from Anonymous on November 1, 2006 8:14:53 AM EST
thank you. u help me alot.
Comment from syaza on April 15, 2007 11:43:19 PM EST
This works great... One disappointing thing is it is not disabling images in the form...
Comment from Anonymous on April 20, 2007 11:31:56 AM EST
TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1116340367337