Hi Anil,
I used your javascript below as part of a program which put the form
values in a database. The problem is it puts the numeric value of the
selected item in, rather than the actual text.
Could you tell me why it does this, and how to fix it please?
Many thanks,
Hazel
> Mark,
>
> Below is a working version of the list box update code.
>
> Instead of using separate arrays for each listbox entry I suggest a
> multidimensional array (an array of arrays). This way you don't need to
use
> the "eval" expression.
>
> Also, instead of resetting the .text value for each entry in the
listbox, I
> suggest removing the contents of the listbox completely and adding in the
> new entries. This will allow you to have a different number of values in
> each array.
>
> Cheers,
> Anil
>
> Sevina Technologies
> www.Sevina.com
>
> ==================CUT HERE===============================
> <HTML>
> <HEAD>
> <TITLE></TITLE>
> <SCRIPT langauge=javascript>
> var aLBValues = new Array() ;
> aLBValues[0] = new Array("poodle","puli","greyhound");
> aLBValues[1] = new Array("trout", "mackerel", "bass");
> aLBValues[2] = new Array("robin", "hummingbird", "crow");
>
> function addLBElement(oLB, value, text)
> {
> var oNewOption = new Option(text) ;
> oNewOption.value = value;
>
> oLB.options[oLB.options.length] = oNewOption ;
> }
>
> function updateLB()
> {
> var oLBSource = document.the_form.choose_category ;
> var oLBDest = document.the_form.the_examples ;
> var nSelId = oLBSource[oLBSource.selectedIndex].value ;
>
> oLBDest.length = 0 ; // Clear the LB
>
> for(nLoop = 0; nLoop < aLBValues[nSelId].length; nLoop++)
> {
> addLBElement(oLBDest, nLoop, aLBValues[nSelId][nLoop]) ;
> }
> }
> function onLoad()
> {
> updateLB() ;
> }
> </SCRIPT>
>
> </HEAD>
> <BODY onLoad="onLoad()">
>
> <form name="the_form">
>
> <select name="choose_category" onChange="updateLB()">
> <option value=0 selected>Dogs
> <option value=1>Fish
> <option value=2>Birds
> </select>
>
> <select name="the_examples">
> <option>DUMMY VALUES
> <option>DUMMY VALUES
> <option>DUMMY VALUES
> </select>
> </form>
>
> </BODY>
> </HTML>
> ==================END CUT================================
>
>
> ----- Original Message -----
> From: <mark.woodward1@b...>
> To: "javascript" <javascript@p...>
> Sent: Thursday, February 15, 2001 6:03 PM
> Subject: [javascript] Dropdown list problem
>
>
> > Hi My name is Mark Woodward. I am having a problem
> > with designing a mini search form for my Web
> > application. What I want to do is select an option from
> > my dropdown list and depending on what I select will
> > depend on what I can select in my second dropdown list.
> > Here is my code below. Please help me on this matter.
> > All ideas would be more than welcome.
> >
> > <HTML>
> > <HEAD>
> > <META NAME="GENERATOR" Content="Microsoft Visual
> > Studio 6.0">
> > <TITLE></TITLE>
> > </HEAD>
> > <BODY>
> >
> > <form name="the_form">
> >
> > <select name="choose_category"
> > onChange="swapOptions
> > (window.document.the_form.choose_category.options
> > [selecteditems].text;)">
> >
> > <option selected>Dogs
> >
> > <option>Fish
> >
> > <option>Birds
> >
> > </select>
> >
> >
> >
> > <select name="the_examples">
> >
> > <option>poodle
> >
> > <option>puli
> >
> > <option>greyhound .
> >
> > </select>
> >
> > </form>
> >
> > <%
> >
> > var dogs = new Array("poodle","puli","greyhound");
> >
> > var fish = new Array("trout", "mackerel", "bass");
> >
> > var birds = new Array("robin", "hummingbird", "crow");
> >
> > function swapOptions(the_array_name)
> >
> > {
> >
> > var numbers_select
> > window.document.the_form.the_examples;
> >
> > var the_array = eval(the_array_name);
> >
> > setOptionText
> > (window.document.the_form.the_examples, the_array);
> >
> > }
> >
> > function setOptionText(the_select, the_array)
> >
> > {
> >
> > for (loop=0; loop <
> > the_select.options.length; loop++)
> >
> > {
> >
> > the_select.options[loop].text
> > the_array[loop];
> >
> > }
> >
> > }
> >
> > the_select.options[0].text = the_array[0];
> >
> > %>
> >
> > </BODY>
> > </HTML>
> >
> > If you can solve the problem. Then I would be really
> > grateful if you could send me an email with the source
> > code for the working solution.
> >
> > Best regards Mark.