Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Creating conditional (cascading) drop-down lists using JavaScript
  3.  * @author Kristina Marasovic
  4.  */
  5.  
  6. var CollegeProgram = {
  7.     "Select a College": "college", //this is being used for select ID
  8.     "RIT Croatia": {
  9.         "Select a Campus": "campus", //this is being used for ID
  10.         "Dubrovnik": {
  11.             "Select a Program": "program",
  12.             "Information Technology (BS)": 0,
  13.             "International Hospitality and Service Management (BS)": 0,
  14.             "Digital Business Minor": 0
  15.         },
  16.         "Zagreb": {
  17.             "Select a Program": "program",
  18.             "Information Technology (BS)": 0,
  19.             "International Business (BS)": 0,
  20.             "Service Leadership and Innovation (MS)": 0,
  21.             "Digital Business Minor": 0
  22.         }
  23.     },
  24.     "RIT": {
  25.         "Select a Campus": "campus", //this is being used for ID
  26.         "GCCIS - Information Sciences & Technologies": {
  27.             "Select a Program": "program",
  28.             "Web & Mobile Computing (BS)": 0,
  29.             "Human Centered Computing (BS)": 0,
  30.             "Computing & Information Technologies (BS)": 0
  31.         },
  32.         "GCCIS - Computer Science": {
  33.             "Select a Program": "program",
  34.             "Computer Science (BS)": 0,
  35.             "Computer Science (MS)": 0
  36.         }
  37.     }
  38. };
  39.  
  40. var selects;
  41.  var result="";
  42. window.onload = resetForm;
  43.  
  44. function resetForm () {
  45.     selects = document.getElementById("selects");
  46.     addSelectFor(CollegeProgram);
  47. };
  48.  
  49. /**
  50.  * Recursive building of conditional selects.
  51.  *
  52.  * @param {type} options data structure containing select options
  53.  */
  54. function addSelectFor(options) {
  55.     //1. stop condition
  56.    
  57.     if (Object.keys(options).length <= 1) {
  58.       result.appendChild(document.createTextNode(output));
  59.         //console.log("Bla: " + output);
  60.         return;
  61.     }
  62.  
  63.     //2. if the select element does not exist, create it
  64.     var selectElement = document.getElementById(options[Object.keys(options)[0]]);
  65.     if (!selects.contains(selectElement)) {
  66.         selectElement = document.createElement('select');
  67.         selectElement.setAttribute("id", options[Object.keys(options)[0]]); //the value of the first object property
  68.         selects.appendChild(selectElement);
  69.        
  70.     }
  71.  
  72.     //3. clear select entries before adding new ones, use a for-in loop
  73.     selectElement.length = 0;
  74.     for (var key in options) {
  75.         selectElement.options.add(new Option(key, key));
  76.        
  77.     }
  78.  
  79.     //4. implement onchange event handling - recursive call
  80.    
  81.          
  82.  
  83.     selectElement.onchange = function () {
  84.        
  85.         if (this.selectedIndex < 1) {
  86.             var sibling = this.nextSibling;
  87.             while (sibling) {
  88.                 sibling.length = 1;
  89.                 sibling = sibling.nextSibling;
  90.             }
  91.             return;
  92.         }
  93.         output += this.value + " ";
  94.         addSelectFor(options[this.value]);
  95.  
  96.          
  97.     };
  98.    
  99. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement