View Full Version : Resolved Adding a value to a drop list item

11-15-2010, 03:50 PM
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.

<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;





<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 name="SelectUser" ID=Select2>

<option value="1">Choose User</option>



Philip M
11-15-2010, 04:27 PM
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

11-15-2010, 04:54 PM

11-15-2010, 05:42 PM
I found I can also do it like this:-

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