What I want is that when I select a category in dropdown Type
, my second dropdown Item
should get dynamically updated, on the change event of Type
.
If I will be able to access the Array I have created inside the change event, I will be able to get the exact values in my second dropdown.
But I'm not sure how to extract the values from there.
var type = ['fruit', 'fruit', 'fruit', 'veg', 'veg', 'veg', 'chinese', 'chinese', 'chinese', 'chinese'];
var item = ['apple', 'banana', 'orange', 'potatao', 'capsicum', 'carrot', 'noodles', 'momos', 'spring roll', 'soup'];
var select = document.getElementById("jqfrt");
for (var i = 0; i < type.length; i++) {
var opt = type[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
};
$('#jqfrt').change(function(event, informationObj) {
var selectedValue = $("#jqfrt").val();
var itemsnew = [];
for (var i = 0; i < type.length; i++) {
if (selectedValue == type[i]) {
itemsnew.push(item[i]);
};
};
console.log(itemsnew);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="jqfrt" id="jqfrt"></select>
<select name="jqitm" id="jqitm"></select>
I'm slightly new to JavaScript a simple help will be appreciated! Thanks.