Homer_J
Homer_J

Reputation: 3323

Drop down option incremental values

EDIT - Thanks all - I knew it would be simple...couldn't see the wood from the trees!

var abc = new Array('Perth','Auckland');

case '1':
    document.getElementById(q15).options.length = 0;
    for (i = 0; i < abc.length; i++) {
        createOption(document.getElementById(q15), abc[i], abc.[i]);
        }
break;

I am sure this is easy but I'm having a mental block.

When the above Javascript outputs a drop down with the values from the array, it produces the following:

<option value="Perth">Perth</option>
<option value="Auckland">Auckland</option>

However, I really need the following:

<option value="1">Perth</option>
<option value="2">Auckland</option>

Where the value= number increments for all the options.

Any ideas?

Thanks,

H.

Upvotes: 0

Views: 520

Answers (3)

user583726
user583726

Reputation: 685

use i instead of abc[i] OR abc.[i]

Upvotes: 0

JMM
JMM

Reputation: 26807

Assuming the second argument to your createOption function is the value of the value attribute:

createOption(document.getElementById(q15), i + 1, abc[i]);

Upvotes: 0

Rich
Rich

Reputation: 2174

Looks like this call sets the values:

createOption(document.getElementById(q15), abc[i], abc.[i]);

So try changing either the first or second abc[i] to just i.

Upvotes: 1

Related Questions