469,344 Members | 5,548 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,344 developers. It's quick & easy.

populating a drop down using another drop down

27
hii guyzz
Im doing a project which requires that i have a drop down for the country and i require to populate another drop down for state.
Oct 31 '06 #1
6 11943
hii guyzz
Im doing a project which requires that i have a drop down for the country and i require to populate another drop down for state.
Hi,

You can try this code snippet. I may be helpful.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Country Selection</title>
  4. <script language="javascript">
  5. var Country = null;
  6. function init()
  7. {
  8.     var numCountry = 2;
  9.     var oStateSlct = document.CountrySelect.Function;
  10.     Country = new Array(numCountry+1); 
  11.     oStateSlct.options[0] = new Option("Please select a Country Type", "-1");
  12.     Country[0] = new Array(0); 
  13.     oStateSlct.options[1] = new Option("Country1", "1");
  14.     Country[1] = new Array(1);
  15.     Country[1][0] = new Option("State1", "-1");
  16.     oStateSlct.options[2] = new Option("Country2", "2");
  17.     Country[2] = new Array(2);
  18.     Country[2][0] = new Option("State1", "-1");
  19.     Country[2][1] = new Option("State2", "0");
  20.  
  21. }
  22.  
  23. function fillUpSelectControl(srcCntrl,targetCntrl,selectOptions)
  24. {
  25.     var slctIndx = srcCntrl.options.selectedIndex;
  26.     for( i=0; i<targetCntrl.options.length; i++ )
  27.         targetCntrl.options[i] = null;
  28.  
  29.     var newOptions = selectOptions[slctIndx]; 
  30.     for( i=0; i<newOptions.length; i++ )
  31.         targetCntrl.options[i] = new Option( newOptions[i].text, newOptions[i].value );
  32.     targetCntrl.options[0].selected = true;
  33. }
  34. </script>
  35. </head>
  36. <body onload="init();">
  37.  
  38. <form name="CountrySelect" >
  39. <select NAME="Function" onChange="fillUpSelectControl(this,this.form.StateType,Country)">
  40. </select>
  41. <select name="StateType">
  42. </select>
  43. </form>                
  44.  
  45. </body> 
  46. </html>
Nov 1 '06 #2
spooky
27
hii
thanks for the snippet , i've been doin this project using Servlets,JSP and JDBC.
I dont know much about scripting how can i connect to db using this
anil
Nov 1 '06 #3
iam_clint
1,208 Expert 1GB
You would probably have to use AJAX, DOM you can find help for that all over google good examples.
Nov 1 '06 #4
spooky
27
Will this code work if yes can i get the value of hidden Name to my jsp page!!


function madeSelection(selObj)
{
var selectedValue = selObj.options[selObj.selectedIndex].value;
//document.Name.value = "ANIL";
document.hiddenName.value = selectedValue;
document.write(document.hiddenName.value);
}
html tag------
input type=hidden name=hiddenName
Nov 3 '06 #5
r035198x
13,262 8TB
Will this code work if yes can i get the value of hidden Name to my jsp page!!


function madeSelection(selObj)
{
var selectedValue = selObj.options[selObj.selectedIndex].value;
//document.Name.value = "ANIL";
document.hiddenName.value = selectedValue;
document.write(document.hiddenName.value);
}
html tag------
input type=hidden name=hiddenName
No. This will not work.
The culprit line is
Expand|Select|Wrap|Line Numbers
  1.  document.hiddenName.value = selectedValue;
which should be
Expand|Select|Wrap|Line Numbers
  1.  document.formName.hiddenName.value = selectedValue;
or
Expand|Select|Wrap|Line Numbers
  1.  
  2. document.forms['formName'].hiddenName.value = selectedValue;
  3.  
Where formName is the name of the form on which the hidden input is.
Yes you can get to this value in a jsp using
Expand|Select|Wrap|Line Numbers
  1.  String name = request.getParameter("hiddenName");
Nov 3 '06 #6
Hi ,
ur code is really excellent. can u just give me
the code by taking the values of countries and states from the mysql db. is it possible to take the values from the db..?(mysql and jsp). can pls reply back immediately....thanks in advance..
Sep 16 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by msnews.microsoft.com | last post: by
1 post views Thread by asad | last post: by
4 posts views Thread by Pete Lux | last post: by
2 posts views Thread by Jim in Arizona | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.