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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    50
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Adding a value to a drop list item

    This is the code that I have, on selecting the first item in the first list it populates the second list with users. What I need to do is create a "value" for each of the items in the second list.

    Cheers Dan.

    Code:
    <html> 
    <head> 
    <title>Create Form Elements</title> 
     
    <script language="javascript"> 
    <!--
     
    function CreateUsers() {
     
      var Primary = document.newforms.site.selectedIndex;
     
      if ((Primary == null) || (Primary == 0)) return;
     
      if (Primary == 1) {
     
      var Users = new Array;
      Users[0] = new Option("user1");
      Users[1] = new Option("user2");
      Users[2] = new Option("user3");
      Users[3] = new Option("user4");
      Users[4] = new Option("user5");
      Users[5] = new Option("user6");
      Users[6] = new Option("user7");
      Users[7] = new Option("user8");
      Users[8] = new Option("user9");
      Users[9] = new Option("user10");
      Users[10] = new Option("user11");
      Users[11] = new Option("user12");
      Users[12] = new Option("user13");
     
      }
     
      for (i=document.newforms.SelectUser.options.length; i>0; i--) { 
    	document.newforms.SelectUser.options[i] = null;
      }
     
      for(i=0; i<Users.length; i++) {
    	document.newforms.SelectUser.options[i] = Users[i];
      }
     
      document.newforms.SelectUser.options[0].selected = true;
     
    }
     
    
    </script> 
     
     
    </head> 
     
    <body> 
     
    <form name="newforms" ID=Form1> 
     
    Choose Country:
     
    <select name="site" onChange="CreateUsers()" ID=Select1> 
     
      <option value="">Choose Site</a> 
      <option value="site1">Site1</a> 
      <option value="site2">Site2</a> 
     
    </select> 
     
    <select name="SelectUser" ID=Select2> 
     
      <option value="1">Choose User</option> 
     
    </select> 
     
    </form> 
    </body> 
    </html>
    Last edited by DanielHowden; 11-15-2010 at 04:55 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    Code:
    for (i=0; i<Users.length; i++) {
    document.newforms.SelectUser.options[i] = Users[i];
    document.newforms.SelectUser.options[i].value = "user" + (i+1);
    // alert (document.newforms.SelectUser.options[i].value);
    }
    There's going to be an influx of poorer people leaving London. - BBC Breakfast

  • Users who have thanked Philip M for this post:

    DanielHowden (11-15-2010)

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    50
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Thanks

  • #4
    New Coder
    Join Date
    Nov 2010
    Posts
    50
    Thanks
    16
    Thanked 0 Times in 0 Posts
    I found I can also do it like this:-

    Code:
    Users[2] = new Option("user3", "UserValue");


  •  

    Posting Permissions

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