I created two blocks where I want to populate based on the first select. At this moment, only the first block is self-populating. I want both blocks to work independently. Thanks.
PS: The number of div.block is dynamic, they can be more or less than two blocks
cars = new Array("Mercedes", "Volvo", "BMW", "porche");
phones = new Array('Samsung', 'Nokia', 'Iphone');
names = new Array('Kasper', 'Elke', 'Fred', 'Bobby', 'Frits');
colors = new Array('blue', 'green', 'yellow');
populateSelect();
$(function() {
$('#cat').change(function() {
populateSelect();
});
});
function populateSelect() {
cat = $('#cat').val();
$('#item').html('');
eval(cat).forEach(function(t) {
$('#item').append('<option>' + t + '</option>');
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="block">
<select id="cat">
<option val="cars">cars</option>
<option val="phones">phones</option>
</select>
<select id="item">
</select>
</div>
<div class="block">
<select id="cat">
<option val="names">names</option>
<option val="colors">colors</option>
</select>
<select id="item">
</select>
</div>