title: Using JavaScript to filter a drop down menu
body: <p>Based on <a href="https://stackoverflow.com/questions/10570904/use-jquery-to-change-a-second-select-list-based-on-the-first-select-list-option">post here on StackOverflow</a>, I am trying to make a drop down menu, which contents depends on the selection of another drop down menu.</p>
  
  <p>Fortunately the solution I found, is very applicable to my problem, although I originally do get my drop down menu options from a database. Less fortunately however, is that I cannot seem to get the solution to work.</p>
  
  <p>Both menus are correctly read from the database, and converted to the correct HTML-code. So what seems to fail is the JavaScript which is used to filter the options in the second drop down menu. My code is as following:</p>
  
  <pre><code>&lt;div id="selection"&gt;
      &lt;?php
          // Top level part selection
          echo "&lt;select name='firstLevelParts' id='select1'&gt;";
          foreach ($category_primary as $category_item):
              echo "&lt;option value = '".$category_item['cat_primary_id']."'&gt;".$category_item['cat_primary_name']."&lt;/option&gt;";
          endforeach;
          echo "&lt;/select&gt;";
  
  
          // Second level part selection (This should be updated, based on selection in top level menu)
          echo "&lt;select name='secondLevelParts' id='select2'&gt;";
          foreach ($category_secondary as $cat_sec_item):
               echo "&lt;option value='".$cat_sec_item['cat_secondary_code']."'&gt;".$cat_sec_item['cat_secondary_name']."&lt;/option&gt;"; 
          endforeach;
          echo "&lt;/select&gt;";
      ?&gt;
  
  &lt;/div&gt;
  
  
  &lt;script&gt;
      $("#select1").change(function() { 
          if(typeof $(this).data('options') === "undefined")){
              /*Taking an array of all options-2 and kind of embedding it on the select1*/
              $(this).data('options',$('#select2 option').clone());
              } 
          var id = $(this).val();
          var options = $(this).data('options').filter('[value=' + id + ']');
          $('#select2').html(options);
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>I really hope you can help me locating the flaw in my code, I really annoys the heck out of me...</p>
  
  <p>Update - Per request, here is the resulting HTML-code, I can't really give you the page url, as it lies behind password protection:</p>
  
  <pre><code>&lt;div id="selection"&gt;
  &lt;select name='firstLevelParts' id='select1'&gt;
   &lt;option value = '1'&gt;Terræn&lt;/option&gt;
   &lt;option value = '2'&gt;Fundamentsystem&lt;/option&gt;
   &lt;option value = '3'&gt;Vægsystem&lt;/option&gt;
   &lt;option value = '4'&gt;Dæksystem&lt;/option&gt;
   &lt;option value = '5'&gt;Tagsystem&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;select name='secondLevelParts' id='select2'&gt;
   &lt;option value='1'&gt;Jordprofil&lt;/option&gt;
   &lt;option value='2'&gt;Befæstet Areal&lt;/option&gt;
   &lt;option value='3'&gt;Beplantning&lt;/option&gt;
   &lt;option value='1'&gt;Fundamentkonstruktion&lt;/option&gt;
   &lt;option value='2'&gt;Bærelag&lt;/option&gt;
   &lt;option value='3'&gt;Åbning&lt;/option&gt;
   &lt;option value='4'&gt;Lukning&lt;/option&gt;
   &lt;option value='5'&gt;Inddækning&lt;/option&gt;
   &lt;option value='6'&gt;Afslutning&lt;/option&gt;
   &lt;option value='7'&gt;Afskærmning&lt;/option&gt;
   &lt;option value='8'&gt;Fuge&lt;/option&gt;
   &lt;option value='9'&gt;Samling&lt;/option&gt;
   &lt;option value='10'&gt;Overflade&lt;/option&gt;
  &lt;/select&gt;  
  &lt;/div&gt;
  
  &lt;script&gt;
      $("#select1").change(function() { 
          if(typeof $(this).data('options') === "undefined")){
              /*Taking an array of all options-2 and kind of embedding it on the select1*/
              $(this).data('options',$('#select2 option').clone());
              } 
          var id = $(this).val();
          var options = $(this).data('options').filter('[value=' + id + ']');
          $('#select2').html(options);
      });
  &lt;/script&gt; 
  </code></pre>