Edit: Correct the answer as per Rory McCrossan's comment(added append() with jQuery object), and apologize for misleading.
The issue is about the if statement, your code would only append one <option> that is if i equals 3.
jQuery API Documetation
According to the documentation, you have several options to do so...
1) Use append() with jQuery object
for (var i = 0; i < 10; i++) {
$('#mySelect').append($('<option>', {
value: i,
text: i,
selected: i == 3 ? true : false
}))
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="mySelect"></select>
2) Use appendTo()
for (var i = 0; i < 10; i++) {
$('<option>', {
value: i,
text: i,
selected: i == 3 ? true : false
}).appendTo($('#mySelect'))
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="mySelect"></select>
3) Use append() with a method
for (var i = 0; i < 10; i++) {
$('#mySelect').append(CreateOption(i))
}
function CreateOption(i) {
return $('<option>', {
value: i,
text: i,
selected: i == 3 ? true : false
})
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="mySelect"></select>
4) Use append() with text
for (var i = 0; i < 10; i++) {
var selected = i==3 ? ' selected=true' : ''
var option = '<option value='+ i + selected + '>' + i + '</option>'
$('#mySelect').append(option)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="mySelect"></select>