Reputation: 405
My situation is:
my mark up is
<div class="multi-field">
<select class="text-one" name="destination[]">
<option selected value="base">Please Select</option>
<option value="colombo">Colombo</option>
<option value="kandy">Kandy</option>
<option value="anuradhapura">Anuradhapura</option>
</select>
<br />
<select class="text-two" name="attraction_or_activity[]">
<option value="attraction">Attraction</option>
<option value="activity">Activity</option>
</select>
<select id="populated_attr_or_activity" name="attraction_or_activity_selected[]">
<!-- here I have to populate the ARRAYS as option -->
</select>
</div>
And my jquery is
<script>
$(document).ready(function() {
$(".text-two").change(function() {
/* saving selected values in variables */
var selected_destination = $('.text-one :selected').val();
var selected_attraction_or_activity = $('.text-two :selected').val();
colombo_attractions = new Array("Ganga Ramaya","National Art Gallery","Galle Face Green","Arcade Indepentent Square");
colombo_activities = new Array("City Walk 2016","Traditional Dance Competition 2016","Local Spicy food");
if ( selected_destination == 'colombo' && selected_attraction_or_activity == 'attraction') {
colombo_attractions.forEach(function(t) {
$('#populated_attr_or_activity').append('<option>'+t+'</option>');
});
}
if ( selected_destination == 'colombo' && selected_attraction_or_activity == 'activity') {
colombo_activities.forEach(function(t) {
$('#populated_attr_or_activity').append('<option>'+t+'</option>');
});
}
});
});
</script>
I almost get it. For when the user is select the 2nd (attraction/activity) dropdown list the 3rd is loaded according to the selection. it loads the 'activities' which can be found in 'colombo'
issue: when the user switches back to the other choice of attraction or activity dropdown list, the 3rd drop down list geeting added with options. But I want to remove the last loaded options and add back the options according to the new attraction/activity dropdown list selection.
Here is the demo : http://codepen.io/foolishcoder7721/pen/oboKML
How can I fix this and get it as I explained above?
Upvotes: 0
Views: 53
Reputation: 1816
Empty third list on top:
$(document).ready(function() {
$(".text-two").change(function() {
$('#populated_attr_or_activity').html('');
/* saving selected values in variables */
var selected_destination = $('.text-one :selected').val();
var selected_attraction_or_activity = $('.text-two :selected').val();
colombo_attractions = new Array("Ganga Ramaya","National Art Gallery","Galle Face Green","Arcade Indepentent Square");
colombo_activities = new Array("City Walk 2016","Traditional Dance Competition 2016","Local Spicy food");
if ( selected_destination == 'colombo' && selected_attraction_or_activity == 'attraction') {
colombo_attractions.forEach(function(t) {
$('#populated_attr_or_activity').append('<option>'+t+'</option>');
});
}
if ( selected_destination == 'colombo' && selected_attraction_or_activity == 'activity') {
colombo_activities.forEach(function(t) {
$('#populated_attr_or_activity').append('<option>'+t+'</option>');
});
}
});
});
Upvotes: 1