I'm currently working with an array and attempting to add a new <option>
element for each item in the array.
Here is the approach I have taken:
array.forEach(function(arr) {
console.log(array);
$(".filterSection .department").append('<option value="'+array+'">'+array+'</option>');
});
However, the issue lies in the fact that when the options are generated, all values in the array are displayed within each <option>
. Although new option
tags are created, they end up containing all items from the array as their value and text.
Check out the complete demo below:
var array = ["Value 1", "Value 2", "Value 3"];
array.forEach(function(arr) {
console.log(array);
$(".department").append('<option value="' + array + '">' + array + '</option>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="department" name="department" id="department"></select>
Could you please point out where I might be making a mistake?