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[i] = new Option(city[i]);
}
}
</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 6 2155
Nick wrote: document.form.city.options[i] = new Option(city[i]);
This creates options... <option value="Los Angeles">Los Angeles</option>
Instead id like it to be... <option value="0">Los Angeles</option>
Use
new Option(text, value)
--
Martin Honnen http://JavaScript.FAQTs.com/
Nick wrote: 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[i] = new Option(city[i]); } } </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], i, false, false);
<url: http://devedge.netscape.com/library/...ce/option.html
/>
<url: http://www.devguru.com/Technologies/...ef/option.html
/> (although their documentation is a bit outdated, recommending calling
history.go(0) after populating a <select> programmatically)
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Nick wrote: 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[i] = new Option(city[i]); } } </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[i] = new Option(city[i],i);
}
}
</script>
Mick
"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:40***************@agricoreunited.com... Read the documention for the Option object, you'll see that it takes four parameters, not one. Use:
new Option(city[i], i, false, false);
<url: http://devedge.netscape.com/library/...ce/option.html /> <url: http://www.devguru.com/Technologies/...ef/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.
--
Jason, aka The Blue Raja
Blue Raja wrote: "Grant Wagner" <gw*****@agricoreunited.com> wrote in message news:40***************@agricoreunited.com... Read the documention for the Option object, you'll see that it takes four parameters, not one. Use:
new Option(city[i], i, false, false);
<url: http://devedge.netscape.com/library/...ce/option.html /> <url: http://www.devguru.com/Technologies/...ef/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.
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:40**************@agricoreunited.com... Read the documention for the Option object, you'll see that it takes
four parameters, not one. Use:
new Option(city[i], i, false, false);
<url: http://devedge.netscape.com/library/...ce/option.html /> <url:
http://www.devguru.com/Technologies/...ef/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?
--
Jason, aka The Blue Raja This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jeffrey Silverman |
last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree
structure from that list.
Basically, I want to turn this:
$list = array(
array( 'id' => 'A', 'parent_id' => null, 'value'...
|
by: Venkatesh |
last post by:
Hi Everybody,
This is the first timw I am entering into this Group.
I am developing a VB Project with an MDI form.
I want to display IE Favorites into my application. For this I need to...
|
by: Gurry |
last post by:
Hi there
I would like to write a javascript function that creates a drop-down
list dynamically. I read on the docs that most HTML controls can not
be created on run-time. what kind of...
|
by: Steve |
last post by:
I'm sure this question has been asked before, but I couldn't seem to get
the right combination of words to get a hit.
I'm looking to do one of two things...
1) Take a list of items, and have...
|
by: Alvaro G Vicario |
last post by:
I have a list built on HTML and CSS:
<ul>
<li>Foo</li>
<li>Bar
<ul>
<li>Gee</li>
</ul>
</li>
</ul>
|
by: paulakeijzers |
last post by:
I've got a problem with asp.net i am trying to make a menu control. and have searched the web for serveral controls but they don't work correctly.
I am pretty new to asp.net building. What am i...
|
by: weiwei |
last post by:
<% Option Explicit %>
<!--#include file="includes/conn.inc"-->
<% Dim rds, ID %>
<% Set rds = Server.CreateObject("ADODB.Recordset") %>
<% rds.Open "select RecID, LocationName from Location ORDER...
|
by: Richard Gromstein |
last post by:
Hello, I have an exercise that I need to finish very soon and I really
need help understanding what to do and how exactly to do it. I am
working on reading the chapter right now and working on it...
|
by: krishna81m |
last post by:
I am unable to create a list of objects dynamically. I run a SQL query on the database and populate objects of types that I also know aprior. I read the query as a string, run it on the database and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |