SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Body
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to dynamically disable form elements

    I'm trying to dynamicly disable certain form elements after a user selects a checkbox on a form. My form that is loaded is populated with dynamic elements, and the elements that need to be disabled will also be dynamic. As such, I've populated an array with the elements that are valid to potentially be disabled. When a user selects a checkbox, I call a function to determine if the OTHER form checkboxes should be disabled or not. The problem that I'm having is that I can't figure out how to dynamicly disable those elements.

    For instance, if I have a checkbox named "bike" that needs to be disabled, then I can input this, and it will work fine:

    function disableElements(){
    document.pre.bike.disabled=true;
    }


    However, if I populate an array, and substitute "bike" for it's position in the array, it doesn't work. Something like this is what I need:

    function disableElements(){
    formElements=new Array();
    formElements[0]="Orange";
    formElements[1]="Car";
    formElements[2]="Bike";
    document.pre.formElements[2].disabled=true;
    }

    I've simplified the code here but this is the gist of what needs to happen. The array is working properly and if I print the data to screen from that function it is correct.The "formElements[2]" will actually need to be something like "formElements[i]" given that it will be nested in an if statement checking to see where the form element should be disabled or not.

    Thank in advance for your help.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,863
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    document.pre[formElements[2]].disabled=true;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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