choudhmh
asked on
Chained Select Drop Down Menu
Hi Guys,
I have two drop down list. I want to write a chained select where a option from dropdown list1 is chosen then its correspondent option from dropdown list2 should show up in dropdown list2.
Can some one direct me on how i would go about writting this in Javascript and asp.net.
Dropdown list 1
USA
UK
Drop down list 2
If UK then i want options London, Birmingham and Manchester to show up
If USA then i want Washington, New York and Los Angelas to show up.
If usa is chosen i dont want drop down list 2 to show any options of UK and vice versa.
Does any one know how i would go about writting the javascript function , arrays and variable for this programme.
Thanks
I have two drop down list. I want to write a chained select where a option from dropdown list1 is chosen then its correspondent option from dropdown list2 should show up in dropdown list2.
Can some one direct me on how i would go about writting this in Javascript and asp.net.
Dropdown list 1
USA
UK
Drop down list 2
If UK then i want options London, Birmingham and Manchester to show up
If USA then i want Washington, New York and Los Angelas to show up.
If usa is chosen i dont want drop down list 2 to show any options of UK and vice versa.
Does any one know how i would go about writting the javascript function , arrays and variable for this programme.
Thanks
I've noticed that adding the option element to a select causes some off effects in Mozilla and Netscape (they do not like innerText to be added), the inner text becomes invisible :S
see this example
<html>
<head>
</head>
<body>
<script>
function getcars()
{
document.getElementById('c ars').leng th = null
if(document.getElementById ('makers') .options[d ocument.ge tElementBy Id('makers ').selecte dIndex].va lue == "a")
{
var a = document.getElementById('c ars').leng th
var opt = new Option('FORD','FORD','FORD ')
var opt1 = new Option('GM','GM','GM')
document.getElementById('c ars').opti ons[0] = opt;
document.getElementById('c ars').opti ons[1] = opt1;
}
if(document.getElementById ('makers') .options[d ocument.ge tElementBy Id('makers ').selecte dIndex].va lue == "g")
{
var a = document.getElementById('c ars').leng th
var opt = new Option('MERCEDES','MERCEDE S','MERCED ES')
document.getElementById('c ars').opti ons[0] = opt;
}
if(document.getElementById ('makers') .options[d ocument.ge tElementBy Id('makers ').selecte dIndex].va lue == "j")
{
var a = document.getElementById('c ars').leng th
var opt = new Option('TOYOTA','TOYOTA',' TOYOTA')
var opt1 = new Option('HONDA','HONDA','HO NDA')
var opt2 = new Option('MITSIBISHI','MITSI BISHI','MI TSIBISHI')
document.getElementById('c ars').opti ons[0] = opt;
document.getElementById('c ars').opti ons[1] = opt1;
document.getElementById('c ars').opti ons[2] = opt2;
}
}
</SCRIPT>
<body onload = "getcars()">
<form>
<select id="makers" name="makers" onchange="getcars()">
<option value="a">American</option >
<option value="g">German</option>
<option value="j">Japanese</option >
</select>
<select id="cars" name="cars">
</select>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<script>
function getcars()
{
document.getElementById('c
if(document.getElementById
{
var a = document.getElementById('c
var opt = new Option('FORD','FORD','FORD
var opt1 = new Option('GM','GM','GM')
document.getElementById('c
document.getElementById('c
}
if(document.getElementById
{
var a = document.getElementById('c
var opt = new Option('MERCEDES','MERCEDE
document.getElementById('c
}
if(document.getElementById
{
var a = document.getElementById('c
var opt = new Option('TOYOTA','TOYOTA','
var opt1 = new Option('HONDA','HONDA','HO
var opt2 = new Option('MITSIBISHI','MITSI
document.getElementById('c
document.getElementById('c
document.getElementById('c
}
}
</SCRIPT>
<body onload = "getcars()">
<form>
<select id="makers" name="makers" onchange="getcars()">
<option value="a">American</option
<option value="g">German</option>
<option value="j">Japanese</option
</select>
<select id="cars" name="cars">
</select>
</form>
</body>
</html>
For ur example
<html>
<head>
</head>
<body>
<script>
function getcars()
{
document.getElementById('c ars').leng th = null
if(document.getElementById ('makers') .options[d ocument.ge tElementBy Id('makers ').selecte dIndex].va lue == "a")
{
var a = document.getElementById('c ars').leng th
var opt = new Option('NEW YORK','NEW YORK','NEW YORK')
var opt1 = new Option('WASHINGTON','WASHI NGTON','WA SHINGTON')
var opt2 = new Option('LOS ANGELES','LOS ANGELES','LOS ANGELES')
document.getElementById('c ars').opti ons[0] = opt;
document.getElementById('c ars').opti ons[1] = opt1;
document.getElementById('c ars').opti ons[2] = opt2;
}
if(document.getElementById ('makers') .options[d ocument.ge tElementBy Id('makers ').selecte dIndex].va lue == "g")
{
var a = document.getElementById('c ars').leng th
var opt = new Option('LONDON','LONDON',' LONDON')
var opt1 = new Option('BRIMINGHAM','BRIMI NGHAM','BR IMINGHAM')
var opt2 = new Option('MANCHESTER','MANCH ESTER','MA NCHESTER')
document.getElementById('c ars').opti ons[0] = opt;
document.getElementById('c ars').opti ons[1] = opt1;
document.getElementById('c ars').opti ons[2] = opt2;
}
}
</SCRIPT>
<body onload = "getcars()">
<form>
<select id="makers" name="makers" onchange="getcars()">
<option value="a">USA</option>
<option value="g">UK</option>
</select>
<select id="cars" name="cars">
</select>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<script>
function getcars()
{
document.getElementById('c
if(document.getElementById
{
var a = document.getElementById('c
var opt = new Option('NEW YORK','NEW YORK','NEW YORK')
var opt1 = new Option('WASHINGTON','WASHI
var opt2 = new Option('LOS ANGELES','LOS ANGELES','LOS ANGELES')
document.getElementById('c
document.getElementById('c
document.getElementById('c
}
if(document.getElementById
{
var a = document.getElementById('c
var opt = new Option('LONDON','LONDON','
var opt1 = new Option('BRIMINGHAM','BRIMI
var opt2 = new Option('MANCHESTER','MANCH
document.getElementById('c
document.getElementById('c
document.getElementById('c
}
}
</SCRIPT>
<body onload = "getcars()">
<form>
<select id="makers" name="makers" onchange="getcars()">
<option value="a">USA</option>
<option value="g">UK</option>
</select>
<select id="cars" name="cars">
</select>
</form>
</body>
</html>
ASKER
Guys,
Theres only one problem my data on the first drop down list is pouplated from the database. It retrives more then one country and using the above script say i chose USA, nothing on the second drop down list comes up. Is there any other way of doing this without declaring the option - <option VALUE>- Select Country -</option>
<option VALUE="us">United States</option>
<option VALUE="uk">United Kingdom</option>
as this will be automatically retrived from the database.
I dont want to put array of contries as the country will be related tro users feedback thats retrived from the databse.
Thanks
Mac
Theres only one problem my data on the first drop down list is pouplated from the database. It retrives more then one country and using the above script say i chose USA, nothing on the second drop down list comes up. Is there any other way of doing this without declaring the option - <option VALUE>- Select Country -</option>
<option VALUE="us">United States</option>
<option VALUE="uk">United Kingdom</option>
as this will be automatically retrived from the database.
I dont want to put array of contries as the country will be related tro users feedback thats retrived from the databse.
Thanks
Mac
check this
<html>
<head>
</head>
<body>
<script>
function getcountry()
{
document.getElementById('c ountry').l ength = null
if(document.getElementById ('countrie s').option s[document .getElemen tById('cou ntries').s electedInd ex].value == "usa")
{
var a = document.getElementById('c ountry').l ength
var opt = new Option('NEW YORK','NEW YORK','NEW YORK')
var opt1 = new Option('WASHINGTON','WASHI NGTON','WA SHINGTON')
var opt2 = new Option('LOS ANGELES','LOS ANGELES','LOS ANGELES')
document.getElementById('c ountry').o ptions[0] = opt;
document.getElementById('c ountry').o ptions[1] = opt1;
document.getElementById('c ountry').o ptions[2] = opt2;
}
if(document.getElementById ('countrie s').option s[document .getElemen tById('cou ntries').s electedInd ex].value == "uk")
{
var a = document.getElementById('c ountry').l ength
var opt = new Option('LONDON','LONDON',' LONDON')
var opt1 = new Option('BRIMINGHAM','BRIMI NGHAM','BR IMINGHAM')
var opt2 = new Option('MANCHESTER','MANCH ESTER','MA NCHESTER')
document.getElementById('c ountry').o ptions[0] = opt;
document.getElementById('c ountry').o ptions[1] = opt1;
document.getElementById('c ountry').o ptions[2] = opt2;
}
}
</SCRIPT>
<body onload = "getcountry()">
<form>
<select id="countries" name="countries" onchange="getcountry()">
<option value="usa">USA</option>
<option value="uk">UK</option>
</select>
<select id="country" name="country">
</select>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<script>
function getcountry()
{
document.getElementById('c
if(document.getElementById
{
var a = document.getElementById('c
var opt = new Option('NEW YORK','NEW YORK','NEW YORK')
var opt1 = new Option('WASHINGTON','WASHI
var opt2 = new Option('LOS ANGELES','LOS ANGELES','LOS ANGELES')
document.getElementById('c
document.getElementById('c
document.getElementById('c
}
if(document.getElementById
{
var a = document.getElementById('c
var opt = new Option('LONDON','LONDON','
var opt1 = new Option('BRIMINGHAM','BRIMI
var opt2 = new Option('MANCHESTER','MANCH
document.getElementById('c
document.getElementById('c
document.getElementById('c
}
}
</SCRIPT>
<body onload = "getcountry()">
<form>
<select id="countries" name="countries" onchange="getcountry()">
<option value="usa">USA</option>
<option value="uk">UK</option>
</select>
<select id="country" name="country">
</select>
</form>
</body>
</html>
ASKER
Hi archrajan
Still it does not function, i think its got to do with the
<select id="countries" name="countries" onchange="getcountry()">
<option value="usa">USA</option>
<option value="uk">UK</option>
</select>
As on my one its like this:
<asp:DropDownList id="txtcurrentsupplier" DataValueField="TelephoneS upplierID" DataTextField="TelephoneSu pplierName " Runat="server" onchange="getcountry()">
<asp:ListItem Value="usa">USA</asp:ListI tem>
<asp:ListItem Value="uk">UK</asp:ListIte m>
</asp:DropDownList></DIV>
As you can see the value on this fields are being retrived from the database depending on the details. What ways do you think i need to change the javascript so it automaticaly recognis ethe frist drop down value and according to that it inserts its correspondent value on drop down2. I know arrays would be involve, but how will i make sure these arrays are called when a value from drop down 1 is selected.
Still it does not function, i think its got to do with the
<select id="countries" name="countries" onchange="getcountry()">
<option value="usa">USA</option>
<option value="uk">UK</option>
</select>
As on my one its like this:
<asp:DropDownList id="txtcurrentsupplier" DataValueField="TelephoneS
<asp:ListItem Value="usa">USA</asp:ListI
<asp:ListItem Value="uk">UK</asp:ListIte
</asp:DropDownList></DIV>
As you can see the value on this fields are being retrived from the database depending on the details. What ways do you think i need to change the javascript so it automaticaly recognis ethe frist drop down value and according to that it inserts its correspondent value on drop down2. I know arrays would be involve, but how will i make sure these arrays are called when a value from drop down 1 is selected.
did u change all the id's in ur code .
can u post ur full code please?
can u post ur full code please?
country and countries
country is the id for the second drop down box
and
countries is the id for the first drop down box
country is the id for the second drop down box
and
countries is the id for the first drop down box
ASKER
<script language="JavaScript">
function getcountry()
{
document.getElementById('T elephoneSu pplierName ').length = null
if(document.getElementById ('Telephon eSupplierN ame').opti ons[docume nt.getElem entById('T elephoneSu pplierName ').selecte dIndex].va lue == "BT")
{
var a = document.getElementById('T elephoneSu pplierName ').length
var opt = new Option('NEW YORK','NEW YORK','NEW YORK')
var opt1 = new Option('WASHINGTON','WASHI NGTON','WA SHINGTON')
var opt2 = new Option('LOS ANGELES','LOS ANGELES','LOS ANGELES')
document.getElementById('T elephoneSu pplierName ').options [0] = opt;
document.getElementById('T elephoneSu pplierName ').options [1] = opt1;
document.getElementById('T elephoneSu pplierName ').options [2] = opt2;
}
if(document.getElementById ('Telephon eSupplierN ame').opti ons[docume nt.getElem entById('T elephoneSu pplierName ').selecte dIndex].va lue == "NT")
{
var a = document.getElementById('T elephoneSu pplierName ').length
var opt = new Option('LONDON','LONDON',' LONDON')
var opt1 = new Option('BRIMINGHAM','BRIMI NGHAM','BR IMINGHAM')
var opt2 = new Option('MANCHESTER','MANCH ESTER','MA NCHESTER')
document.getElementById('T elephoneSu pplierName ').options [0] = opt;
document.getElementById('T elephoneSu pplierName ').options [1] = opt1;
document.getElementById('T elephoneSu pplierName ').options [2] = opt2;
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout " onload = "getcountry()">
<form id="contact" method="post" runat="server">
<asp:Panel id="pnlcurrentsupplier" runat="server" visible="True">
<P>
<DIV>
<asp:DropDownList id="txtcurrentsupplier" DataValueField="TelephoneS upplierID" DataTextField="TelephoneSu pplierName " Runat="server" onchange="getcountry()">
<asp:ListItem Value="BT">BT</asp:ListIte m>
<asp:ListItem Value="NT">NT</asp:ListIte m>
</asp:DropDownList></DIV>
<DIV>
<asp:DropDownList id="TelephoneSupplierName" Runat="server" DataValueField="TelephoneC allPlanNam e" DataTextField="TelephoneCa llPlanName " onchange="getcountry()"></ asp:DropDo wnList>
</DIV>
function getcountry()
{
document.getElementById('T
if(document.getElementById
{
var a = document.getElementById('T
var opt = new Option('NEW YORK','NEW YORK','NEW YORK')
var opt1 = new Option('WASHINGTON','WASHI
var opt2 = new Option('LOS ANGELES','LOS ANGELES','LOS ANGELES')
document.getElementById('T
document.getElementById('T
document.getElementById('T
}
if(document.getElementById
{
var a = document.getElementById('T
var opt = new Option('LONDON','LONDON','
var opt1 = new Option('BRIMINGHAM','BRIMI
var opt2 = new Option('MANCHESTER','MANCH
document.getElementById('T
document.getElementById('T
document.getElementById('T
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout
<form id="contact" method="post" runat="server">
<asp:Panel id="pnlcurrentsupplier" runat="server" visible="True">
<P>
<DIV>
<asp:DropDownList id="txtcurrentsupplier" DataValueField="TelephoneS
<asp:ListItem Value="BT">BT</asp:ListIte
<asp:ListItem Value="NT">NT</asp:ListIte
</asp:DropDownList></DIV>
<DIV>
<asp:DropDownList id="TelephoneSupplierName"
</DIV>
<asp:DropDownList id="txtcurrentsupplier" DataValueField="TelephoneS upplierID" DataTextField="TelephoneSu pplierName " Runat="server" onchange="getcountry()">
here the id should be
TelephoneSupplierName
here the id should be
TelephoneSupplierName
and here
<asp:DropDownList id="TelephoneSupplierName" Runat="server" DataValueField="TelephoneC allPlanNam e" DataTextField="TelephoneCa llPlanName " onchange="getcountry()"></ asp:DropDo wnList>
change the id to "TelephoneSupplierName1" or something and same in the script
right now in the script u have
both id's of the 2 drop down boxes same...
<asp:DropDownList id="TelephoneSupplierName"
change the id to "TelephoneSupplierName1" or something and same in the script
right now in the script u have
both id's of the 2 drop down boxes same...
ASKER
i cant do that as this is being retrived from the database. Also dropdown2 id is TelephoneSupplierName. my backend code uses this to retrive the user specified values from the database.
ASKER
tried your suggestion, drop down 1 does not have any vlaue in the list. Also runtime error is thrown at the javascript function
ok use this id TelephoneSupplierName for dropdown 2
and what is the id u are using for drop down 1?
that error is because the id is not referenced properly
and what is the id u are using for drop down 1?
that error is because the id is not referenced properly
ASKER
i've tried your suggestion drop downlist one TelephoneSupplierName and dropdown list2 TelephoneSupplierName 1. I've been told another way of doing this. The suppliers are dependent on regions so say uk is region 2 and usa is region 1. the first drop down list retrivew suppliers to drop down list1 according to the region submitted by user when they register. Is there anyway i could ask the Javacsript to use region to identify supplier. then using the supplier region this will show the supplier name from where drop2 will list the variables of that supplier. Drop down 2 needs to be an array of list, like present.
so u will have 3 drop down menus?
if so u can handle it...
but u have to use unique id's
if so u can handle it...
but u have to use unique id's
ASKER
no theres only one two drop down. the only problem i'm facing at present is with the first one. the values are retrived from the databse according to regions. drop down two is need to be in cvhained selection where if BT is retrived according to user specification then evry city realted to BT needs to be displayed in drop2. Drop2 has no linkage with any database.
yeah thats what the code now does...
though the options are coming from the drop down...u need to know the values before hand to do any manipulations with it..
give the id's of both ur dropdowns now and i can show u an example..
though the options are coming from the drop down...u need to know the values before hand to do any manipulations with it..
give the id's of both ur dropdowns now and i can show u an example..
ASKER
<asp:DropDownList id="txtcurrentsupplier" DataValueField="TelephoneS upplierID" DataTextField="TelephoneSu pplierName " Runat="server" onchange="getcountry()">
<asp:ListItem Value="BT"></asp:ListItem>
<asp:ListItem Value="NT"></asp:ListItem>
</asp:DropDownList></DIV>
<DIV>
<asp:Label ID="Plan" Runat="server" Text="Your Current Call plan">
<asp:DropDownList id="TelephoneSupplierName" Runat="server" DataValueField="TelephoneC allPlanNam e" DataTextField="TelephoneCa llPlanName " onchange="getcountry()"></ asp:DropDo wnList></a sp:Label>
</DIV>
<asp:ListItem Value="BT"></asp:ListItem>
<asp:ListItem Value="NT"></asp:ListItem>
</asp:DropDownList></DIV>
<DIV>
<asp:Label ID="Plan" Runat="server" Text="Your Current Call plan">
<asp:DropDownList id="TelephoneSupplierName"
</DIV>
check this out now
<html>
<head>
</head>
<body>
<script>
function getTelephoneSupplierName()
{
document.getElementById('T elephoneSu pplierName ').length = null
if(document.getElementById ('txtcurre ntsupplier ').options [document. getElement ById('txtc urrentsupp lier').sel ectedIndex ].value == "usa")
{
var a = document.getElementById('T elephoneSu pplierName ').length
var opt = new Option('NEW YORK','NEW YORK','NEW YORK')
var opt1 = new Option('WASHINGTON','WASHI NGTON','WA SHINGTON')
var opt2 = new Option('LOS ANGELES','LOS ANGELES','LOS ANGELES')
document.getElementById('T elephoneSu pplierName ').options [0] = opt;
document.getElementById('T elephoneSu pplierName ').options [1] = opt1;
document.getElementById('T elephoneSu pplierName ').options [2] = opt2;
}
if(document.getElementById ('txtcurre ntsupplier ').options [document. getElement ById('txtc urrentsupp lier').sel ectedIndex ].value == "uk")
{
var a = document.getElementById('T elephoneSu pplierName ').length
var opt = new Option('LONDON','LONDON',' LONDON')
var opt1 = new Option('BRIMINGHAM','BRIMI NGHAM','BR IMINGHAM')
var opt2 = new Option('MANCHESTER','MANCH ESTER','MA NCHESTER')
document.getElementById('T elephoneSu pplierName ').options [0] = opt;
document.getElementById('T elephoneSu pplierName ').options [1] = opt1;
document.getElementById('T elephoneSu pplierName ').options [2] = opt2;
}
}
</SCRIPT>
<body onload = "getTelephoneSupplierName( )">
<form>
<select id="txtcurrentsupplier" name="txtcurrentsupplier" onchange="getTelephoneSupp lierName() ">
<option value="usa">USA</option>
<option value="uk">UK</option>
</select>
<select id="TelephoneSupplierName" name="TelephoneSupplierNam e">
</select>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<script>
function getTelephoneSupplierName()
{
document.getElementById('T
if(document.getElementById
{
var a = document.getElementById('T
var opt = new Option('NEW YORK','NEW YORK','NEW YORK')
var opt1 = new Option('WASHINGTON','WASHI
var opt2 = new Option('LOS ANGELES','LOS ANGELES','LOS ANGELES')
document.getElementById('T
document.getElementById('T
document.getElementById('T
}
if(document.getElementById
{
var a = document.getElementById('T
var opt = new Option('LONDON','LONDON','
var opt1 = new Option('BRIMINGHAM','BRIMI
var opt2 = new Option('MANCHESTER','MANCH
document.getElementById('T
document.getElementById('T
document.getElementById('T
}
}
</SCRIPT>
<body onload = "getTelephoneSupplierName(
<form>
<select id="txtcurrentsupplier" name="txtcurrentsupplier" onchange="getTelephoneSupp
<option value="usa">USA</option>
<option value="uk">UK</option>
</select>
<select id="TelephoneSupplierName"
</select>
</form>
</body>
</html>
ASKER
This is more or less what i'm looking for, but the main problem is on select id txtcurrentsupplier i cant declare any options as these would be automatically retrived from the databse accroding to the region selected by the user they are in. How would i pass that main hurdle. I cant declare any options.
ASKER
How would i write a script within the function where say if region id passed on is 1 from page on show only BT on dropdown list1, if region id passed on from previous page is 2 then only show ntl as the option on drop down list1.
This is how i'm passin gthe region id:
Response.Redirect("Homepag e.aspx?rid =" & TelephoneRegionID & "&tid=" & tid)
This is how i'm passin gthe region id:
Response.Redirect("Homepag
u create a hidden variable
<input type = "hidden" name = "regionvariable" value = "<%=TelephoneRegionID%>">
and pass that region id to this hidden field.
THEN ACCESS THE REGION ID from the hidden field.
document.formname.regionva riable.val ue
a = document.formname.regionva riable.val ue
oh, now do u want to populate the dropdown 1 also via javascript?????
<input type = "hidden" name = "regionvariable" value = "<%=TelephoneRegionID%>">
and pass that region id to this hidden field.
THEN ACCESS THE REGION ID from the hidden field.
document.formname.regionva
a = document.formname.regionva
oh, now do u want to populate the dropdown 1 also via javascript?????
ASKER
I think it would be best if this is not poulated via JS. So it is automattically called from the database using the hidden field
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<html>
<head>
<script LANGUAGE="javaScript">
function populateSTATE(theSel){
zs = theSel.value;
var optState = theSel.form.state.options;
optState.length = 0;
if(State[theSel.value]){
for (var i=0; i<State[theSel.value].leng
optState[optState.length] = new Option (State[zs][i], State[zs][i], true, true);
}
}
theSel.form.state.selected
}
State = new Array();
State["uk"] = ["London","Manchester","Bi
State["us"] = [ "Washington" , "New York","Los Angelas" ];
</script>
</head>
<body>
<form name="contact">
<select NAME="country" onChange="populateSTATE(th
<option VALUE>- Select Country -</option>
<option VALUE="us">United States</option>
<option VALUE="uk">United Kingdom</option>
</select>
<select NAME="state">
<option VALUE><< Select Country First</option>
</select>
</form>
</body>
</html>
//https://www.experts-exchange.com/questions/20816361/Country-Array.html