Creating list menus dynamically...

Discussion in 'Javascript' started by Nick, Jul 7, 2004.

  1. Nick

    Nick Guest

    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
    Nick, Jul 7, 2004
    #1
    1. Advertising

  2. Nick wrote:


    > document.form.city.options = new Option(city);


    > 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/
    Martin Honnen, Jul 7, 2004
    #2
    1. Advertising

  3. Nick

    Grant Wagner Guest

    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 = 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)

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, Jul 7, 2004
    #3
  4. Nick

    Mick White Guest

    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 = 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
    Mick White, Jul 7, 2004
    #4
  5. Nick

    Blue Raja Guest

    "Grant Wagner" <> wrote in message
    news:...
    > 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.

    --

    Jason, aka The Blue Raja
    Blue Raja, Jul 8, 2004
    #5
  6. Nick

    Grant Wagner Guest

    Blue Raja wrote:

    > "Grant Wagner" <> wrote in message
    > news:...
    > > 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.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Grant Wagner, Jul 8, 2004
    #6
  7. Nick

    Blue Raja Guest

    "Grant Wagner" <> wrote in message
    news:...
    > > > 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?

    --

    Jason, aka The Blue Raja
    Blue Raja, Jul 9, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Creating menus

    =?Utf-8?B?U2FuZHk=?=, Jan 29, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    531
    Mark Rae
    Jan 29, 2005
  2. paulakeijzers

    creating dynamic menus with submenus from sql

    paulakeijzers, Nov 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,497
    Mr Newbie
    Nov 8, 2005
  3. Braga

    Creating menus at runtime

    Braga, Jun 18, 2004, in forum: Java
    Replies:
    4
    Views:
    813
    Yu SONG
    Jun 20, 2004
  4. news.west.cox.net

    Select menus above flyout menus? help?

    news.west.cox.net, Dec 8, 2004, in forum: HTML
    Replies:
    8
    Views:
    779
  5. =?Utf-8?B?THZCb2hlbWlhbg==?=

    MasterPages - Dynamically Created Menus Disappearing

    =?Utf-8?B?THZCb2hlbWlhbg==?=, Oct 23, 2006, in forum: ASP .Net
    Replies:
    23
    Views:
    792
    =?Utf-8?B?THZCb2hlbWlhbg==?=
    Oct 24, 2006
Loading...

Share This Page