Dynamic Population Dropdownlistbox

Have a form which is having two combo boxes. My requirement is that when I chose a particular value in combo, then another combo should get filled onchange event of the first combo. Example, the first combo has department names and the second combo should get filled with the sub-department names which are stored in the database. I do not want to submit my form. How to achieve this?

Sample Coding:



var plabels = new Array(); 

var parents = new Array(); 

var family = new Array(); 

parents[0] = new Array( 

"Parent 0 - children 1", 
"Parent 1 - children 2", 
"Parent 2 - children 3"); 

parents[1] = new Array( 

"Parent 1 - children 1", 
"Parent 1 - children 2", 
"Parent 1 - children 3", 
"Parent 1 - children 4"); 

parents[2] = new Array( 

"Parent 2 - children 1", 
"Parent 2 - children 2"); 

parents[3] = new Array( 

"Parent 3 - children 1", 
"Parent 3 - children 2", 
"Parent 3 - children 3"); 

parents[4] = new Array( 

"Parent 4 - children 1", 
"Parent 4 - children 2", 
"Parent 4 - children 3", 
"Parent 4 - children 4"); 

for (i=0; i<5; i++) { 

plabels[i] = "Parent " + i; 


function fillParents() { 

var pList = document.frm.parent; 

for (i=0; i<plabels.length; i++) { 

pList.options[i+1] = new Option(plabels[i], i); 


function fillChilds() { 

var pObj = document.frm.parent; 

var cObj = document.frm.child; 

var parent = pObj.options[pObj.selectedIndex].value; 

var childs = parents[parent]; 

cObj.length = 0; 

for (i=0; i<childs.length; i++) { 

cObj.options[i+1] = new Option(childs[i], i); 

cObj.options[0] = new Option("- Please specify -", "", false, true); 




<p>Dynamic pull-down boxes</p> 

<form name="frm" method="post"> 


<select name="parent" onchange="fillChilds()"> 

<option value="">- Please specify -</option> 



<select name="child"> 

<option value="">- Please specify -</option> 








See also
Use A Layout And Arrange Objects

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.