select an item on a dropdown list

P

passion_to_be_free

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
 
A

ASM

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);">
 
M

Matt Kruse

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/
 
J

JDS

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.
 
P

passion_to_be_free

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
 
J

Jonathan N. Little

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);
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top