Click here to get back home

select an item on a dropdown list

 HomeNewsGroups | Search | About
 comp.infosystems.www.authoring.html    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
select an item on a dropdown list passion_to_be_free 07-27-2005
Posted by passion_to_be_free on July 27, 2005, 9:35 am
Please log in for more thread options


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


Posted by Matt Kruse on July 27, 2005, 1:06 pm
Please log in for more thread options


passion_to_be_free@hotmail.com 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



Posted by JDS on July 27, 2005, 2:10 pm
Please log in for more thread options


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 | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/


Posted by passion_to_be_free on July 27, 2005, 11:16 am
Please log in for more thread options


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


Posted by JDS on July 27, 2005, 2:36 pm
Please log in for more thread options


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%21

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

later...

--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/


Similar ThreadsPosted
best way to put page list on item-list page April 20, 2008, 2:11 am
Can I prevent an item from scrolling with the page? December 7, 2005, 12:56 pm
grey-out/disabled item in listbox March 10, 2006, 11:22 pm
Questionnaire about item layouts for uni project website June 26, 2007, 8:03 pm
Customizable dropdown menus? February 2, 2008, 10:24 pm
dropdownlist border and dropdown arrow? July 16, 2007, 12:16 am
Dropdown menu javascript menus a bad choice? July 22, 2005, 10:06 pm
Select on my DIV November 16, 2005, 4:16 pm
February 19, 2005, 10:21 pm
Very Long Select !!! May 10, 2006, 2:13 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap