www.webdeveloper.com
Results 1 to 2 of 2

Thread: Select Box Dependency Problem

  1. #1
    Join Date
    Jun 2014
    Posts
    1

    Select Box Dependency Problem

    I know how to do select box dependency when 2-3 boxes are involved, but whenever I go beyond that. I end up in a situation where boxes won't populate. Here is the code.


    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function populate(s1,s2){
    	var s1 = document.getElementById(s1);
    	var s2 = document.getElementById(s2);
    	s2.innerHTML = "";
    	if(s1.value == "Chevy"){
    		var optionArray = ["|","camaro|Camaro","corvette|Corvette","impala|Impala"];
    	} else if(s1.value == "Dodge"){
    		var optionArray = ["|","avenger|Avenger","challenger|Challenger","charger|Charger"];
    	} else if(s1.value == "Ford"){
    		var optionArray = ["|","mustang|Mustang","shelby|Shelby"];
    	}
    	for(var option in optionArray){
    		var pair = optionArray[option].split("|");
    		var newOption = document.createElement("option");
    		newOption.value = pair[0];
    		newOption.innerHTML = pair[1];
    		s2.options.add(newOption);
    	}
    }
    
    function populate1(s2,s3){
    	var s2 = document.getElementById(s2);
    	var s3 = document.getElementById(s3);
    	s3.innerHTML = "";
    	if(s2.value == "camaro"){
    		var optionArray = ["|","Red|Red","Black|Black"];
    	} else if(s2.value == "avenger"){
    		var optionArray = ["|","Pink|Pink","Brown|Brown"];
    	
    	for(var option in optionArray){
    		var pair = optionArray[option].split("|");
    		var newOption = document.createElement("option");
    		newOption.value = pair[0];
    		newOption.innerHTML = pair[1];
    		s3.options.add(newOption);
    	}
    }
    
    function populate2(s3,s4){
    	var s3 = document.getElementById(s3);
    	var s4 = document.getElementById(s4);
    	s4.innerHTML = "";
    	if(s3.value == "Red"){
    		var optionArray = ["|","Big|Big","Small|Small"];
    	} else if(s3.value == "Black"){
    		var optionArray = ["|","Tiny|Tiny","Massive|Massive"];
    	
    	for(var option in optionArray){
    		var pair = optionArray[option].split("|");
    		var newOption = document.createElement("option");
    		newOption.value = pair[0];
    		newOption.innerHTML = pair[1];
    		s4.options.add(newOption);
    	}
    }
    
    </script>
    </head>
    <body>
    
    
    <select id="car" name="car" onchange="populate('car','model')">
      <option value="">Choose Car</option>
      <option value="Chevy">Chevy</option>
      <option value="Dodge">Dodge</option>
      <option value="Ford">Ford</option>
    </select>
    
    
    <select id="model" name="model" onchange="populate1('model','colour')">
    	<option value="">Choose a Venue:</option>
    </select>
    
    <select id="colour" name="colour" onchange="populate2('colour','size')">
    	<option value="">Choose a Colour:</option>
    </select>
    
    <select id="size" name="size">
    	<option value="">Choose a Size:</option>
    </select>
    
    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Lightbulb

    Search this forum for examples like this...
    Code:
    <!doctype html>
    <html lang="en">
    <head>
    <title> 1- to 4-level Drop Down</title>
    <meta charset="UTF-8">
    
    <style type="text/css">
    .DDlist { display:none; }
    </style>
    
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?t=202456
    // and: http://www.codingforums.com/showthread.php?t=169465
    // Modified for 1 to 4 (+) level drop down selections
    
    var categories = [];
      categories["startList"] = ["Apparel","Books",'Radio','True','False'];        // Level 1  (True|False is 1 level only)
      
        categories["Apparel"] = ["Men","Women"];                         // Level 2
          categories["Men"] = ["Shirts","Ties","Belts"];                    // Level 3
            categories['Shirts'] = ['Tux','Button-down','Polo',"T's"]         //  Level 4
            categories['Ties'] = ['Silk','String','Bow']                      //  Level 4
            categories['Belts'] = ['Leather','Cloth']                         //  Level 4
    
          categories["Women"] = ["Blouses","Skirts","Scarves"];             // Level 3
            categories["Blouses"] = ["Silk","Cotton","Polyester"];             // Level 4 only
            categories["Skirts"] = ["Full","Pleated"];                         // Level 4 only
            categories["Scarves"] = ["Head","Neck","Waist"];                   // Level 4 only
    	  
        categories["Books"] = ["Biography","Fiction","Nonfiction"];      // Level 2
          categories["Biography"] = ["Contemporary","Historical","Other"];  // Level 3 only
          categories["Fiction"] = ["Science","Romance"];                    // Level 3 only
          categories["Nonfiction"] = ["How-To","Travel","Cookbooks"];       // Level 3 only
    
        categories['Radio'] = ['AM','FM','HD'];                          // Level 2 only
    
    var nLists = 4; // number of lists in the set
    
    function fillSelect(currCat,currList){
      var step = Number(currList.name.replace(/\D/g,""));
      for (i=step; i<nLists+1; i++) {
        document.forms[0]['List'+i].length = 1;
        document.forms[0]['List'+i].selectedIndex = 0;
        document.getElementById('List'+i).style.display = 'none';
      }
      var nCat = categories[currCat];
      if (nCat != undefined) { 
        document.getElementById('List'+step).style.display = 'inline';
        for (each in nCat) 	{
          var nOption = document.createElement('option'); 
          var nData = document.createTextNode(nCat[each]); 
          nOption.setAttribute('value',nCat[each]); 
          nOption.appendChild(nData); 
          currList.appendChild(nOption); 
        }
      } 
    }
    
    function getValues() { 
      var str = '';
      str += document.getElementById('List1').value+'\n';
      for (var i=2; i<=nLists; i++) {
        if (document.getElementById('List'+i).selectedIndex != 0) {
          str += document.getElementById('List'+i).value+'\n'; }
      }
      alert(str); 
    }
    
    function init() { fillSelect('startList',document.forms[0]['List1']); }
    
    navigator.appName == "Microsoft Internet Explorer"
       ? attachEvent('onload', init, false) 
    		   : addEventListener('load', init, false);	
    
    </script>
    </head>
    <body>
    <form action="" onsubmit="return false">
    
    <select name='List1' id="List1" onchange="fillSelect(this.value,this.form['List2'])">
    <option selected>Make a selection</option>
    </select> &nbsp;
    
    <select name='List2' id="List2" onchange="fillSelect(this.value,this.form['List3'])" class="DDlist">
    <option selected>Make a selection</option>
    </select> &nbsp;
    
    <select name='List3' id="List3" onchange="fillSelect(this.value,this.form['List4'])" class="DDlist">
    <option selected >Make a selection</option>
    </select> &nbsp;
    
    <select name='List4' id="List4" class="DDlist">
    <option selected >Make a selection</option>
    </select> &nbsp;
    
    <!-- can add more levels if desired as "List5"+ -->
    <button onclick="getValues()">Show selections</button>
    </form>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles