Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic Drop-Downs to Dynamic Text Info

Newbie
 
Join Date: Nov 2009
Posts: 3
#1: 3 Weeks Ago
I am new to javascript and appreciate any guidance on this issue, so thank you in advance for your advice!

Situation:
I am working with a form that will send information to a database. I need the following:
  1. A drop-down box of options that onChange=...
  2. Opens another drop-down box of unique options that onChange=...
  3. Displays text dependent on which option you chose.
An example:
Choose a Foodgroup: [Drop-down list of options: Fruit, Veggies, Meat, Sweets]
Choose your Favorite: [Depends on which foodgroup]
[Multi-line description of your choice.]

I have found a lot of information about how to make one drop-down list depend on another, and information about how to display text based on a drop-down list. It seems like solutions for each involve arrays... I cannot seem to figure out have to combine the two needs.

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#2: 3 Weeks Ago

re: Dynamic Drop-Downs to Dynamic Text Info


Quote:

Originally Posted by audreyality View Post

I have found a lot of information about how to make one drop-down list depend on another, and information about how to display text based on a drop-down list. It seems like solutions for each involve arrays... I cannot seem to figure out have to combine the two needs.

well, a drop-down list is so-to-speak an HTML representation of an array.

they are basicly built like
  • first <select> is hard coded (by whatever means)
  • depending on the amount of data you either request the next data array (if you have many options) via AJAX or us a hardcoded Array (less options)
  • build a new <select> from that array
  • repeat until the last <select>
as for the text display, simply fill an arbitrary (empty) element of your choice with the appropriate text node according to the last <select>’s value.
Newbie
 
Join Date: Nov 2009
Posts: 3
#3: 3 Weeks Ago

re: Dynamic Drop-Downs to Dynamic Text Info


So a "text node" would be something like this:
document.getElementById('text to be printed')? I am really new to this javascript thing, sorry!

I have an array for each department that associates a list of awards for to that department. I don't know how to associate a description of the award to each award, and that is where I am stuck.

Here is my code this far; I did find it from the URL commented:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml"><head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Honors and Awards Submission</title>
  5. <script type="text/javascript">
  6. //From http://www.alistapart.com/d/complexdynamiclists/dynamicselect.html
  7.         function populate(o)
  8.         {
  9.             d=document.getElementById('Award');
  10.             if(!d){return;}            
  11.             var mitems=new Array();
  12.             mitems['Blank']=[''];
  13.             mitems['Anthropology']=['','Award A','Award B','Award C','Award D'];
  14.             mitems['Art and Art History']=['','Best of Show','Coolest Car'];
  15.             mitems['Biology']=['','Biggest Lab Rat','Safety Goggles of the Year','Lovliest in a Lab Coat','Award Z'];
  16.             mitems['Classical Studies']=['','Best Thesis','Most Likely to Win on Jeopardy'];
  17.             mitems['English']=['','Greatest Grammatical Genius','Perfect Poet','Lovliest Lines','Best at Using Big Words'];
  18.             d.options.length=0;
  19.             cur=mitems[o.options[o.selectedIndex].value];
  20.             if(!cur){return;}
  21.             d.options.length=cur.length;
  22.             for(var i=0;i<cur.length;i++)
  23.             {
  24.                 d.options[i].text=cur[i];
  25.                 d.options[i].value=cur[i];
  26.             }
  27.         }
  28.  
  29.     </script>
  30. </head>
  31.  
  32. <body>
  33. <div id="wrapper">
  34. <form id="form" name="form" method="post" action="">
  35.   <h1>Honors and Awards Submission Form</h1>
  36.   <h3><strong>Attention:</strong> Only fill out one form per recipient. If multiple people will be receiving an award please fill out multiple award forms.</h3>
  37.  
  38. <h2>Choose the Award to File</h2>
  39.  
  40. <label for="Department">Department: <select onchange="populate(this); updateContent(this.value);" id="Department" name="Department">
  41.             <option selected="select" value="Blank"></option>
  42.             <option value="Anthropology">Anthropology</option>
  43.             <option value="Art and Art History">Art and Art History</option>
  44.             <option value="Biology">Biology</option>
  45.             <option value="Classical Studies">Classical Studies</option>
  46.             <option value="English">English</option>
  47.             </select></label>   <br />
  48.             <label for="Award">Award: 
  49.             <select id="Award" name="Award" onchange="updateLikes(this.value);">
  50.             <option selected="selected"></option>
  51.             </select></label>
  52.  
  53. <br />
  54.  
  55.     <label>Award: <br />
  56.        <div>This info needs to be dynamic depending on the award selected.</div>
  57.     </label>
  58. ...
  59. </body>
  60. </html>
  61.  
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#4: 3 Weeks Ago

re: Dynamic Drop-Downs to Dynamic Text Info


Quote:

Originally Posted by audreyality View Post

So a "text node" would be something like this:
document.getElementById('text to be printed')?

Expand|Select|Wrap|Line Numbers
  1. // works even in IE
  2. var TN = document.createTextNode("your text");
  3. document.getElementById("whatever").appendChild(TN);
  4. // or (if supported)
  5. document.getElementById("whatever").textContent = "your text";
I’ll consider the rest tomorrow, as it’s too late today.
Newbie
 
Join Date: Nov 2009
Posts: 3
#5: 3 Weeks Ago

re: Dynamic Drop-Downs to Dynamic Text Info


Here is the solution my co-worker came up with, which works very well! I hope others find it useful too.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.   <script>
  4.     var departments = {};
  5.     var departmentSel;
  6.     var awardSel;
  7.     var descriptionDiv;
  8.  
  9.     function initForm()
  10.     {
  11.       // get a reference to our data table and remove it from the document
  12.       var dataTable  = document.getElementById("dataTable");
  13.       dataTable.parentNode.removeChild(dataTable);
  14.  
  15.       // get the elements we need to change and save them for later
  16.       departmentSel  = document.getElementById("department");
  17.       awardSel       = document.getElementById("award");
  18.       descriptionDiv = document.getElementById("description");
  19.  
  20.       // get an array of rows from the table
  21.       var rows = dataTable.getElementsByTagName('tr');
  22.  
  23.       for (var i=0; i<rows.length; i++)
  24.       { 
  25.         // get an array of columns from each row
  26.         var col = rows[i].getElementsByTagName('td');        
  27.  
  28.         if (col.length && col.length > 2)
  29.         {
  30.           var d = col[0].textContent;
  31.           var a = col[1].textContent;
  32.  
  33.           if (departments[d] == undefined)
  34.           {
  35.             departments[d] = {};
  36.             departments[d].name = d;
  37.             departments[d].awards = {};
  38.           }
  39.           departments[d].awards[a] = {};
  40.           departments[d].awards[a].name = a;
  41.           departments[d].awards[a].description = col[2].textContent;
  42.         }
  43.       }
  44.  
  45.       // add the the "department" options based on our array
  46.       for (var d in departments)
  47.       {
  48.         departmentSel.options[departmentSel.length] = new Option(departments[d].name);
  49.       }
  50.     }
  51.  
  52.     function updateAwards(el)
  53.     {
  54.       var d = departmentSel.options[departmentSel.selectedIndex].text;
  55.  
  56.       // remove any existing award options after the first
  57.       while (awardSel.options.length > 1) awardSel.remove(1);
  58.  
  59.       for (var a in departments[d].awards)
  60.       {
  61.         awardSel.options[awardSel.length] = new Option(departments[d].awards[a].name);
  62.       }
  63.     }
  64.     function updateDescription()
  65.     {
  66.       var d = departmentSel.options[departmentSel.selectedIndex].text;
  67.       var a = awardSel.options[awardSel.selectedIndex].text;
  68.  
  69.       descriptionDiv.innerHTML = departments[d].awards[a].description;
  70.     }
  71.   </script>
  72.   <style>
  73.     div.box { background: #e0e0e0; }
  74.   </style>
  75. </head>
  76. <body onload="initForm()">
  77. <form>
  78.   <select id="department" name="Department" onChange="updateAwards()">
  79.     <option value="">--Select a Department--</option>
  80.   </select>
  81.  
  82.   <select id="award" name="Award" onChange="updateDescription()">
  83.     <option value="">--Choose an Award--</option>
  84.   </select>
  85.   <div id="description" class="box">
  86.   </div>
  87. </form>
  88. <table id="dataTable">
  89. <tr><th>Department</th><th>Award</th><th>Description</th></tr>
  90.  
  91. <tr>
  92.   <td>WITS</td>
  93.   <td>Best use of Lasers Award</td>
  94.   <td>Candidate must demonstrate something cool with lasers</td>
  95. </tr>
  96. <tr>
  97.   <td>WITS</td>
  98.   <td>Gimblepuddy G Horatio Award</td>
  99.  
  100.   <td>Candidate must possess all the requirements necessary to meet the requirements outlined in the criteria</td>
  101. </tr>
  102. <tr>
  103.   <td>Biology</td>
  104.   <td>Best use of Frogs</td>
  105.   <td>Candidate must demonstrate something cool with frogs</td>
  106. </tr>
  107. </table>
  108. </body>
  109.  
  110. </html>
  111.  
Reply