Creating list menus dynamically...

N

Nick

Id like to create some list menus dynamically. The problem is that id
like the option text to be different from the option values. For
example...

<script language=JavaScript>
city[0] = 'Los Angeles';
city[1] = 'New York';
city[2] = 'San Francisco';
city[3] = 'London';
city[4] = 'Paris';
function createCities() {
for (i=0;i < city.length;i++) {
document.form.city.options = new Option(city);
}
}
</script>

This creates options...
<option value="Los Angeles">Los Angeles</option>
<option value="New York">New York</option>
<option value="San Francisco">San Francisco</option>
<option value="London">Lodon</option>
<option value="Paris">Paris</option>

Instead id like it to be...
<option value="0">Los Angeles</option>
<option value="1">New York</option>
<option value="2">San Francisco</option>
<option value="3">Lodon</option>
<option value="4">Paris</option>

How can I do this? -Nick
 
G

Grant Wagner

Nick said:
Id like to create some list menus dynamically. The problem is that id
like the option text to be different from the option values. For
example...

<script language=JavaScript>
city[0] = 'Los Angeles';
city[1] = 'New York';
city[2] = 'San Francisco';
city[3] = 'London';
city[4] = 'Paris';
function createCities() {
for (i=0;i < city.length;i++) {
document.form.city.options = new Option(city);
}
}
</script>

This creates options...
<option value="Los Angeles">Los Angeles</option>
<option value="New York">New York</option>
<option value="San Francisco">San Francisco</option>
<option value="London">Lodon</option>
<option value="Paris">Paris</option>

Instead id like it to be...
<option value="0">Los Angeles</option>
<option value="1">New York</option>
<option value="2">San Francisco</option>
<option value="3">Lodon</option>
<option value="4">Paris</option>

How can I do this? -Nick


Read the documention for the Option object, you'll see that it takes four
parameters, not one. Use:

new Option(city, i, false, false);

<url:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/option.html
/>
<url: http://www.devguru.com/Technologies/ecmascript/quickref/option.html
/> (although their documentation is a bit outdated, recommending calling
history.go(0) after populating a <select> programmatically)
 
M

Mick White

Nick said:
Id like to create some list menus dynamically. The problem is that id
like the option text to be different from the option values. For
example...

<script language=JavaScript>
city[0] = 'Los Angeles';
city[1] = 'New York';
city[2] = 'San Francisco';
city[3] = 'London';
city[4] = 'Paris';
function createCities() {
for (i=0;i < city.length;i++) {
document.form.city.options = new Option(city);
}
}
</script>

This creates options...
<option value="Los Angeles">Los Angeles</option>
<option value="New York">New York</option>
<option value="San Francisco">San Francisco</option>
<option value="London">Lodon</option>
<option value="Paris">Paris</option>

Instead id like it to be...
<option value="0">Los Angeles</option>
<option value="1">New York</option>
<option value="2">San Francisco</option>
<option value="3">Lodon</option>
<option value="4">Paris</option>

How can I do this? -Nick


Others have given you good advice, I would do something like below.

<script type="text/javascript">
city = ['Los Angeles','New York','San Francisco','London','Paris'];
function createCities() {
c= city.length,d=document.formName.city;d.length=0;
for (var i=0;i < c;i++) {
d.options = new Option(city,i);
}
}
</script>

Mick
 
B

Blue Raja

Grant Wagner said:
Read the documention for the Option object, you'll see that it takes four
parameters, not one. Use:

new Option(city, i, false, false);

<url:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/option.html
/>
<url: http://www.devguru.com/Technologies/ecmascript/quickref/option.html
/> (although their documentation is a bit outdated, recommending calling
history.go(0) after populating a <select> programmatically)


Both of these say "new Option([text[, value[, defaultSelected[,
selected]]]])" meaning 0-4 parameters, not 4 only.
 
G

Grant Wagner

Blue said:
Grant Wagner said:
Read the documention for the Option object, you'll see that it takes four
parameters, not one. Use:

new Option(city, i, false, false);

<url:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/option.html
/>
<url: http://www.devguru.com/Technologies/ecmascript/quickref/option.html
/> (although their documentation is a bit outdated, recommending calling
history.go(0) after populating a <select> programmatically)


Both of these say "new Option([text[, value[, defaultSelected[,
selected]]]])" meaning 0-4 parameters, not 4 only.


Where did I say it takes "4 only"?

The intent was clear, if the language wasn't. The Option object contructor takes
additional parameters which allow you to control both the text and value attributes (as
well as the selected and defaultSelected properties).

But points to you for pointing out the obvious, lead by documentation which I included a
link to... way to go, you're a hero.
 
B

Blue Raja

Grant Wagner said:
Read the documention for the Option object, you'll see that it takes four
parameters, not one. Use:

new Option(city, i, false, false);

<url:

http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/option.html
/>
<url: http://www.devguru.com/Technologies/ecmascript/quickref/option.html
/> (although their documentation is a bit outdated, recommending calling
history.go(0) after populating a <select> programmatically)

Both of these say "new Option([text[, value[, defaultSelected[,
selected]]]])" meaning 0-4 parameters, not 4 only.


Where did I say it takes "4 only"?


"...Takes four parameters, not one" implies "4 only". Also, your tone
denoted that you were correcting a mistake.
The intent was clear,

As was mine, i.e. providing IMO helpful advice to what seemed to be a minor
oversight.

Done with your hissy-fit now?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top