I have 5 selections with the same class.
<select class="form-control rating"></select>
<select class="form-control rating"></select>
<select class="form-control rating"></select>
<select class="form-control rating"></select>
<select class="form-control rating"></select>
I want to populate them with dynamic rating values using jQuery. For instance:rateArr=[5,2,2,3,5]
What I really want, for example:
<select class="form-control rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select class="form-control rating">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="form-control rating">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="form-control rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select class="form-control rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
But it was not possible to figure out how to approach the solution. So far I have tried:
$('.rating').each(function(index,row){
for(i=1; i<= rateArr[index]; i++){
$(this).append($("<option</option>").text(i).val(i));
}
});
source
share