View Full Version : Drop Down List Boxes

06-26-2004, 10:42 AM
I'm new to java script and I'm having a problem with writing a variable to store a selected item of a drop down list box. Do I have to write a condition statement like I have had to for the radio buttons I have used, if so how would I word it, for my radio buttons I used;
if (document.forms[0].sex.checked)
gender = "male";
gender = "female";
could I use something simlar to this.... if I did this could I just declare the variable simply as var colour as colour is the name of my drop down list box.

Many thanks.


06-26-2004, 11:38 AM
not 100% sure what you mean... but you could do

var result = document.forms[0].listname.value;


var result = document.forms[0].listname.selectedIndex;

that what you mean?

06-26-2004, 02:59 PM
using var colour = document.forms[0].listname.selectindex; gives me undefined any idea how I colud define it. :thumbsup:

06-26-2004, 03:17 PM
<select name="listname"></select>

06-26-2004, 09:13 PM
No still returning undefined see if anyone can spot what I have done wrong

My variable is: var colour = document.forms[0].colour.selectedindex;

My code to return the value in my table is:
document.write("<tr><td><strong>Colour Selected:</strong></td>");
document.write("<td>" + colour +"</td></tr>");

My code for my drop down list box is:
<select name="colour">

Many thanks


Willy Duitt
06-26-2004, 09:18 PM
It's: selectedIndex
Not: selectedindex

Note the uppercase I.....

06-26-2004, 10:07 PM
this gives me a numerical value ie if Blond is selected it give a value of 1 Brown gives a value of 2 could I use an else if statement to change the value of 1 back to Blond ect. :)

Willy Duitt
06-26-2004, 10:29 PM
Try something like:

04-18-2005, 02:05 AM
Is there a way for me to set another variable on the selection of a drop down item?