Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > select an item on a dropdown list

Reply
Thread Tools

select an item on a dropdown list

 
 
passion_to_be_free@hotmail.com
Guest
Posts: n/a
 
      07-27-2005
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

 
Reply With Quote
 
 
 
 
ASM
Guest
Posts: n/a
 
      07-27-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      07-27-2005
(E-Mail Removed) 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


 
Reply With Quote
 
JDS
Guest
Posts: n/a
 
      07-27-2005
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 | (E-Mail Removed)lid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

 
Reply With Quote
 
passion_to_be_free@hotmail.com
Guest
Posts: n/a
 
      07-27-2005
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

 
Reply With Quote
 
Jonathan N. Little
Guest
Posts: n/a
 
      07-27-2005
(E-Mail Removed) 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
 
Reply With Quote
 
JDS
Guest
Posts: n/a
 
      07-27-2005
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 | (E-Mail Removed)lid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

 
Reply With Quote
 
mcbargainelectronics mcbargainelectronics is offline
Junior Member
Join Date: Dec 2010
Posts: 1
 
      12-07-2010
I used:
document.getElementById('dropdown_menu').options[x].selected = "selected";
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Item in DropDown in DataGrid? localhost ASP .Net 7 01-13-2004 05:58 AM
Setting background list item color in dropdown list control M. Craig ASP .Net Web Controls 1 01-09-2004 03:32 AM
Select dropdown box bleeds into Javascript dropdown menu Mike HTML 1 12-18-2003 09:49 PM
How to set background color of List item in dropdown list M. Craig ASP .Net Web Controls 0 11-12-2003 06:26 PM
No event fired when first item in dropdown select hypomite ASP .Net 2 08-14-2003 01:37 PM



Advertisments