I have 2 select lists and I would like to populate the options in the 2nd list based on the selection of the first list.
This is the first list:
<select class="" id="applicationForm_package">
<option value="no_package">Please select a package</option>
<option package_identifier="package_5" value="5">Package 1</option>
<option package_identifier="package_60" value="60">Package 2</option>
<option package_identifier="package_58" value="58">Package 3</option>
<option package_identifier="package_55" value="55">Package 4</option>
</select>
I then have (in javascript) and variable for each package in the first list:
<script>
var package_5 = "<option value='20'>1 Month</option><option value='110'>6 Months</option><option value='200'>12 Months</option>";
var package_60 = "<option value='25'>1 Month</option><option value='140'>6 Months</option><option value='250'>12 Months</option>";
var package_58 = "<option value='26'>1 Month</option><option value='146'>6 Months</option><option value='270'>12 Months</option>";
var package_55 = "<option value='37'>1 Month</option><option value='212'>6 Months</option><option value='400'>12 Months</option>";
</script>
As the user interacts with the first select list, I would like the options from the respective variables to populate the 2nd list.
Below is the 2nd select and the javascript that I have written. Everything seems to work fine, the issue is the 2nd select list is not getting updated. In my else statement, the console is working but the variable is not defined when I check it in my console.
<select id="applicationForm_subscription">
<option value="no_package_selected">Please select a package First</option>
</select>
<script>
var noPackageSelected = "<option value='no_package_selected'>Please select a package</option>";
jQuery( "select#applicationForm_package" ).change(function() {
var package_name = jQuery( "select#applicationForm_package option:selected").text();
var package_id = jQuery( "select#applicationForm_package option:selected").val();
if(package_id == "no_package"){
jQuery("select#applicationForm_subscription").find("option").remove().end().append(noPackageSelected).val("no_package_selected");
}else{
console.log("update subscriptions");
var get_selected_package_name = jQuery("select#applicationForm_package option:selected").attr('package_identifier');
jQuery("select#applicationForm_subscription").find("option").remove().end().append(get_selected_package_name);
}
});
Any assistance would be great.
Cheers,