select an item on a dropdown list

Discussion in 'HTML' started by passion_to_be_free@hotmail.com, Jul 27, 2005.

  1. Guest

    This is probably simple, but I can't seem to find it anywhere.

    I have have some values stored in javascript variables. I have a
    <select> dropdown list whose options correspond to these values. I want
    to be able to select an item on the dropdown list based on the value of
    the javascript variable.

    Let's say this is my list and my variable:

    <select id='popup'>
    <option value="default">--Please Choose a saying--</option>
    <option value="hello">Hello</option>
    <option value="goodbye">Goodbye</option>
    </select>

    var input = "hello";

    Is there a way to select the 2nd option using using that variable
    value? Something like:

    var popup = document.getElementById("popup");
    popup.selectedItem = input;

    If you have any help or know of a place where I can read up on this, I
    would appreciate it.

    -Benjamin
     
    , Jul 27, 2005
    #1
    1. Advertising

  2. ASM Guest

    wrote:
    > This is probably simple, but I can't seem to find it anywhere.
    >
    > I have have some values stored in javascript variables. I have a
    > <select> dropdown list whose options correspond to these values. I want
    > to be able to select an item on the dropdown list based on the value of
    > the javascript variable.
    >
    > Let's say this is my list and my variable:
    >
    > <select id='popup'>
    > <option value="default">--Please Choose a saying--</option>
    > <option value="hello">Hello</option>
    > <option value="goodbye">Goodbye</option>
    > </select>
    >
    > var input = "hello";
    >
    > Is there a way to select the 2nd option using using that variable
    > value? Something like:
    >
    > var popup = document.getElementById("popup");
    > popup.selectedItem = input;


    to get the selected option value :
    alert('popup choice = '+popup.options[popup.selectedIndex].value);

    to get the selected option item :
    alert('popup choice = '+popup.options[popup.selectedIndex].text);

    with 'hello', to show the option in select :
    for(var i=0;i<popup.length;i)
    if(popup.value=='hello') popup.selectedIndex = i;

    other usefull :

    <select id='popup' onchange=" var k = this.selectedIndex;
    if(i==0) alert('Do other choice');
    else
    alert('choice = '+this.options[k].value);">

    --
    Stephane Moriaux et son [moins] vieux Mac
     
    ASM, Jul 27, 2005
    #2
    1. Advertising

  3. Matt Kruse Guest

    wrote:
    > I have have some values stored in javascript variables. I have a
    > <select> dropdown list whose options correspond to these values. I
    > want to be able to select an item on the dropdown list based on the
    > value of the javascript variable.


    You need to loop through all the options in the select, find the one whose
    value matches your variable value, then mark it as selected.

    Functionality like this is best hidden from view with generalized functions,
    so you can do:

    var input="hello";
    setInputValue(document.getElementById("popup"), input);

    The generalized setInputValue function and others are available if you want
    to take a look:
    http://www.JavascriptToolbox.com/validations/

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
     
    Matt Kruse, Jul 27, 2005
    #3
  4. JDS Guest

    On Wed, 27 Jul 2005 09:35:11 -0700, passion_to_be_free wrote:

    > I have have some values stored in javascript variables. I have a
    > <select> dropdown list whose options correspond to these values. I want
    > to be able to select an item on the dropdown list based on the value of
    > the javascript variable.
    >
    > Let's say this is my list and my variable:
    >
    > <select id='popup'>
    > <option value="default">--Please Choose a saying--</option>
    > <option value="hello">Hello</option>
    > <option value="goodbye">Goodbye</option>
    > </select>
    >
    > var input = "hello";
    >
    > Is there a way to select the 2nd option using using that variable
    > value? Something like:
    >
    > var popup = document.getElementById("popup");
    > popup.selectedItem = input;


    I'm a smidge confused by your description.

    Do you want to have the item in the dropdown list become selected based on
    the value of another JavaScript variable? So that, when this other
    variable, "input", becomes equal to, say, "hello", the second item in
    "popup" becomes selected?

    Or do you just want to get the value of the second item in the "popup"
    drop-down list?

    The two tasks are similar but obviously not exactly the same.

    --
    JDS | lid
    | http://www.newtnotes.com
    DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
     
    JDS, Jul 27, 2005
    #4
  5. Guest

    your first description is what i'm looking for. then if later on the
    javascript variable changed to "goodbye" the function would then change
    the selected value to "goodbye" on the list
     
    , Jul 27, 2005
    #5
  6. wrote:

    > This is probably simple, but I can't seem to find it anywhere.
    >
    > I have have some values stored in javascript variables. I have a
    > <select> dropdown list whose options correspond to these values. I want
    > to be able to select an item on the dropdown list based on the value of
    > the javascript variable.
    >
    > Let's say this is my list and my variable:
    >
    > <select id='popup'>
    > <option value="default">--Please Choose a saying--</option>
    > <option value="hello">Hello</option>
    > <option value="goodbye">Goodbye</option>
    > </select>
    >
    > var input = "hello";
    >
    > Is there a way to select the 2nd option using using that variable
    > value? Something like:
    >
    > var popup = document.getElementById("popup");
    > popup.selectedItem = input;

    <snip>
    There is no 'selectedItem; property for a SELECT element, there is in
    your example 'popup.options.selectedIndex' that would equal the numeric
    index of the selected option. popup.value is what you want

    popup.value=input;

    also for reference:

    if popup.value==input
    then popup.options.selectedIndex==1
    and popup.options[1].value==input
    so below would also work...

    for( var i=0; i<popup.options.length; i++){
    popup.options.selected=(popup.options.value==input);
    }

    --
    Take care,

    Jonathan
    -------------------
    LITTLE WORKS STUDIO
    http://www.LittleWorksStudio.com
     
    Jonathan N. Little, Jul 27, 2005
    #6
  7. JDS Guest

    On Wed, 27 Jul 2005 11:16:55 -0700, passion_to_be_free wrote:

    > your first description is what i'm looking for. then if later on the
    > javascript variable changed to "goodbye" the function would then change
    > the selected value to "goodbye" on the list


    Something like this:

    http://engineering.jhu.edu/~jeff/test_select.html?the_select=&my_value=&submit=change it!

    Remember, there is more than one way to skin a cat. This is just a
    starting point.

    later...

    --
    JDS | lid
    | http://www.newtnotes.com
    DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
     
    JDS, Jul 27, 2005
    #7
  8. mcbargainelectronics

    Joined:
    Dec 6, 2010
    Messages:
    1
    I used:
    document.getElementById('dropdown_menu').options[x].selected = "selected";
     
    mcbargainelectronics, Dec 7, 2010
    #8
    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. hypomite
    Replies:
    2
    Views:
    621
    S. Justin Gengo
    Aug 14, 2003
  2. localhost

    Select Item in DropDown in DataGrid?

    localhost, Dec 22, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    7,428
    Steven Cheng[MSFT]
    Jan 13, 2004
  3. Mike
    Replies:
    1
    Views:
    849
  4. M. Craig

    How to set background color of List item in dropdown list

    M. Craig, Nov 12, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    325
    M. Craig
    Nov 12, 2003
  5. M. Craig
    Replies:
    1
    Views:
    784
    Victor Garcia Aprea [MVP]
    Jan 9, 2004
Loading...

Share This Page