Weird Thoughts From Eric's Head

Tags - Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW

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


it is helpful
This is a sweet bit of code. Thanks for posting it, helped allot!
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!
This is wonderful.
Thank You for this little script. Just what I looked for :D
Thank you!!
Thank you buddy. You made life easy.
GREAT found after hour of googleing!
Thank You So Much. You made it simple and easy.
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
thank you. u help me alot.
This works great... One disappointing thing is it is not disabling images in the form...


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 9 + 10 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1116340367337