473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

select an item on a dropdown list

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 '05 #1
6 10293
ASM
pa****************@hotmail.com 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[i].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
Jul 27 '05 #2
JDS
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 | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 27 '05 #3
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 '05 #4
pa****************@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
Jul 27 '05 #5
pa****************@hotmail.com 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[i].selected=(popup.options[i].value==input);
}

--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Jul 27 '05 #6
JDS
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/tes...t=change+it%21

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

later...

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

Jul 27 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jas | last post by:
I want an ASP page with a dropdown and a simple button. Every time the user chooses an item from the dropdown and clicks on the button i want that value written below in list and allow user to...
1
by: Rod Early | last post by:
I need to know when the select element's dropdown list is opened (as when the user clicks on the arrow or does ALT-downarrow from the keyboard). Similarly, I need to known when the dropdown list...
6
by: passion_to_be_free | last post by:
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...
2
by: hypomite | last post by:
I have an handler for the SelectedIndexChanged event of a dropdown box. I have also set the AutoPostBack option to True. When you select any item besides the first one, the event sucessfully fires....
2
by: DotNetJunkies User | last post by:
hi,, im trying to work out what code is needed for the below problem about databinding a dropdown list and having actual datalist items as value "0" ie 'please select' can anyone tell me how to...
4
by: =?Utf-8?B?c2lMdmVy?= | last post by:
Hhi, I'm working on an asp .net project that have small side panel which 'disallow' long SELECT/dropdown list.. So i could not force them to a certain that shows my longest option, which now...
4
by: Matthew Cox | last post by:
Hi, I've been searching the net all day today trying to figure out how one would accomplish what I originally thought would be a fairly straight forward and simple thing. The problem I'm trying...
2
by: bdbeames | last post by:
Ok, It has been one of those days. Here is my bump in the road. I have an add user form for the administrator. The administrator enters name, password and then selects access level from a...
1
by: RichardR | last post by:
I have a webpage which has a table that contains a column with several drop down boxes (<SELECT>). The contents of the drop down boxes are dynamically populated so I have no idea what the actually...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.