Only when I have the code in an on change event does the list become sorted. I want it to sort when the page loads. I have tried using onload but to no success.
Also I’m trying to make it so whatever the user selects, that value is the first one on the list.
Works…
myDropdown.onchange = function () {
$("#dropdown").append($("#dropdown option").sort(function(a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
}))
myTextBox.value = this.value;
}
Doesn’t Work…
// var myDropdown = $('#dropdown');
// var opts_list = myDropdown.find('option');
// function sortTheList() {
// $("#dropdown").append($("#dropdown option").sort(function(a, b) {
// return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
// }))
// };
// myDropdown.onload = function () {
// $('#dropdown option').sort(function(a,b) { return $(a).text() > $(b).text() ? 1 : -1; });
// myDropdown.html('').append('#dropdown option');
// }
// myDropdown.onload = function () {
// $("#dropdown").on($("#dropdown option").sort(function(a, b) {
// return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
// }))
// }
myDropdownis a jQuery object. It doesn't have anonload, but you can do$(document).ready(function() { ... });