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

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...

I didnt get it, can u explain a bit

I am doing this work in asp.net, two ddlist are asp.net ddlist.
I need to do only the above mentioned thing in Java Script.
PLs help

hi,

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

Check it.

<html>
<head>
 <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');
}
}
</Script>
</head>

<body>
 <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>
</select>
<input type="button" name="go" value="Value Selected"
onclick="alert(document.exform.dropdown1.options[document.exform.dropdown1.selectedIndex].value);">
</div></form>


</body>

</html>

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.