Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    South East UK. 35 miles east of London, in sight of the River Thames.
    Posts
    300
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Disable all form elements

    Hi All,

    I have a form that is populated from a database. To prevent the data from being resubmitted, I want to disable every single form element so that the complete form is read only. If any of the data needs editing and resubmitting, an edit button needs to be clicked on to enable all form elements for editing.

    Does anyone have a script that will cycle through all form element types (irrespective of their 'names', 'id's', etc) to disable/enable enmasse?

    Gary

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here
    Code:
    len=document.formname.length;
    for(x=0;x<len;x++) {
    document.formname.elements[x].disabled=true;
    }

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    South East UK. 35 miles east of London, in sight of the River Thames.
    Posts
    300
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Disable all form elements

    Hi Chris,

    Thanks for the code you sent, it works a treat. I triggered it via an on-load and called it as a function from a button. All worked.

    I then tried to enable the form elements by calling a function from outside the form (but on the same page), as follows:

    len=document.formname.length;
    for(x=0;x<len;x++) {
    document.formname.elements[x].disabled=false ;
    }

    This did not work. What am I failing to understand about this script?

    Regards

    Gary

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    shouldn't it be:

    len=document.formname.elements.length;

    and to make a single function to enable/disable elements:

    function enableDisable(flag){
    //flag = true if disabled, false if enabled
    var f = document.formname;
    var len=f.elements.length;
    for(x=0;x<len;x++) {
    f.elements[x].disabled=flag;
    }
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    South East UK. 35 miles east of London, in sight of the River Thames.
    Posts
    300
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Disable all form elements

    Hi Glenn,

    Thanks, I'll try it out now.

    How do you guys do this so fast? It takes me hours sorting out these things.

    Regards

    Gary

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Using elements is more proper.

    I'd do it like this anyway
    Code:
    for( var elem, i = 0; ( elem = document.formname.elements[i] ); i++ )
    { 
        elem.disabled = false ; 
    }
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #7
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    enable/disable group of form inputs

    I want to have an HTML form that depending on which radio button the user selects, different sets of form inputs are enabled. That is, one set of inputs (checkboxes and text input fields) are enabled when option 1 is selected, a second set is enabled when option 2 is selected, etc.

    I searched the web and forums for this, but all I found was ways to disable/enable the entire form, not a subset of all the elements on the form. I think that the FIELDSET property could help me, but there is no LENGTH attribute for it. Any suggestions?

    Thanks

  • #8
    Regular Coder
    Join Date
    Oct 2004
    Posts
    168
    Thanks
    0
    Thanked 5 Times in 5 Posts
    What if the visitor has disabled JavaScript or is using a non-scripting browser?
    They will be able to submit data maliciously / accidentally.

    Make sure this will not be a mission-critical problem - if so carry out further validation server-side.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •