Based on post here on StackOverflow, I am trying to make a drop down menu, which contents depends on the selection of another drop down menu.
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.
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:
<div id="selection">
<?php
// Top level part selection
echo "<select name='firstLevelParts' id='select1'>";
foreach ($category_primary as $category_item):
echo "<option value = '".$category_item['cat_primary_id']."'>".$category_item['cat_primary_name']."</option>";
endforeach;
echo "</select>";
// Second level part selection (This should be updated, based on selection in top level menu)
echo "<select name='secondLevelParts' id='select2'>";
foreach ($category_secondary as $cat_sec_item):
echo "<option value='".$cat_sec_item['cat_secondary_code']."'>".$cat_sec_item['cat_secondary_name']."</option>";
endforeach;
echo "</select>";
?>
</div>
<script>
$("#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);
});
</script>
I really hope you can help me locating the flaw in my code, I really annoys the heck out of me...
Update - Per request, here is the resulting HTML-code, I can't really give you the page url, as it lies behind password protection:
<div id="selection">
<select name='firstLevelParts' id='select1'>
<option value = '1'>Terræn</option>
<option value = '2'>Fundamentsystem</option>
<option value = '3'>Vægsystem</option>
<option value = '4'>Dæksystem</option>
<option value = '5'>Tagsystem</option>
</select>
<select name='secondLevelParts' id='select2'>
<option value='1'>Jordprofil</option>
<option value='2'>Befæstet Areal</option>
<option value='3'>Beplantning</option>
<option value='1'>Fundamentkonstruktion</option>
<option value='2'>Bærelag</option>
<option value='3'>Åbning</option>
<option value='4'>Lukning</option>
<option value='5'>Inddækning</option>
<option value='6'>Afslutning</option>
<option value='7'>Afskærmning</option>
<option value='8'>Fuge</option>
<option value='9'>Samling</option>
<option value='10'>Overflade</option>
</select>
</div>
<script>
$("#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);
});
</script>