I have a Select HTML Component to which i am adding Options dynamically
This is my code , where the functionality is working fine
Based on the selectedValue i am adding the selected attribute dynamically to the Option
Is there any better way of adding the selected attribute
$(document).ready(function() {
var option = '';
var cars = ["Saab", "Volvo", "BMW"];
var selectedValue = 'Volvo'
for (var i = 0; i < cars.length; i++) {
if (selectedValue && cars[i] == selectedValue)
option += '<option value="' + cars[i] + '" selected>' + cars[i] + '</option>';
else
option += '<option value="' + cars[i] + '">' + cars[i] + '</option>';
}
$('#carscomponent').append(option);
});