I need to disable and enable some drop down list data according to conditions

I have two drop down lists.

Say, First ddlist has A,B,C,D,E,F and second ddlist has 1,2,3,4,5.

When I select C from the first ddlist, then 1,3,5 should be disabled in second ddlist.
And again if I select B from the first ddlist, 1,3,5 will b enabled and now 4 will be disabled in the second ddlist.

How can I get it with java Script pls help

7 Years
Discussion Span
Last Post by naveenl

Hi subhankar02dey,

for this thing you can use the functions in the header where in which when the particular value is selected then the function is called and then we can edit waht to be displayed in the another drop down.

call the function in the select as shown below

<select name="selectname" size="1" onchange="setOptions(document.formname.selectname.options[document.formname.selectname.selectedIndex].value);">

it works...

Edited by naveenl: n/a



OK down is the sample program that changes whenever you select the different index in the dropdown.

Check it.

 <Script Language="JavaScript">
 function setOptions(chosen) {
var selbox = document.exform.dropdown1;
selbox.options.length = 0;
if (chosen == " ") {
  selbox.options[selbox.options.length] = new Option('Please select one of the states',' ');
if (chosen == "1") {
  selbox.options[selbox.options.length] = new Option('BANGALORE','oneone');
  selbox.options[selbox.options.length] = new Option('MYSORE','onetwo');
if (chosen == "2") {
  selbox.options[selbox.options.length] = new Option('CHENNAI','twoone');
  selbox.options[selbox.options.length] = new Option('Coimbatore','twotwo');
if (chosen == "3") {
  selbox.options[selbox.options.length] = new Option('Calicut','threeone');
  selbox.options[selbox.options.length] = new Option('Trivandrum','threetwo');

 <form name="exform"><div align="center">
<select name="dropdown" size="1" onchange="setOptions(document.exform.dropdown.options[document.exform.dropdown.selectedIndex].value);">
<option value=" " selected="selected"> </option>
<option value="1">KARNATAKA</option>
<option value="2">TAMIL NADU</option>
<option value="3">KERALA</option>
</select><br /> <br />
<select name="dropdown1" size="1">
<option value=" " selected="selected">Please select one of the STATES</option>
<input type="button" name="go" value="Value Selected"



Yes, I got it.
But u hv made the items of the ddlists static.
If I need to populate the ddlists by data fetching from database, then wat i hv to do? I mean I want to make it dynamic


Hi subhankar02dey,

For Dynamic data,
1.you need to write an servlet and
2.need to have the JDBC drivers to connect to the database and
3.then you can extract the data from the database.

The working is done as follows:

1.select an index in the dropdown list
2.then that must go to the servlet and it is the thing that identifies the row in the schema of the database and
3.then fetch whatever data you need from that row and you can show in the place of the cities names that i have entered in the header of the jsp page.

Try it.. It will work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.