How can I loop from A to Z? I'd like to populate a select menu with the letters of the alphabet, eg
<select>
<option>A</option>
<option>B</option>
<option>C</option>
...
<option>Z</option>
</select>
Use char codes: JavaScript Char Codes
for (var i = 65; i <= 90; i++) {
$('#select_id_or_class').append('<option>' + String.fromCharCode(i) + '</option>');
}
You can do this with the following
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
$.each(alphabet, function(letter) {
$('.example-select-menu').append($('<option>' + alphabet[letter] + '</option>'));
});
This answer demonstrates the nice idea that you do not need a hardcoded string. In short:
for (i = 65; i <= 90; i++) {
arr[i-65] = String.fromCharCode(i).toLowerCase();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With