So I have a pair of <select>
boxes and have a jQuery script to transfer selected <option>
s from one select box to the other. I also have a function to do the opposite. This works fine except when I "put back" the <option>
s to the first <select>
they are appended to the bottom instead of alphabetically as before.
So I found this function courtesy of this question:
function sortSelect() {
var options = $('#inactiveTickers option');
var arr = options.map(function(_, o) {
return {
t: $(o).text(),
v: o.value
};
}).get();
arr.sort(function(o1, o2) {
return o1.t > o2.t ? 1 : o1.t < o2.t ? -1 : 0;
});
options.each(function(i, o) {
console.log(i);
o.value = arr[i].v;
$(o).text(arr[i].t);
});
}
Then I have this jQuery which captures the click event on a link with id #remove
:
$('#remove').click(function() {
return !$('#activeTickers option:selected').remove().appendTo('#inactiveTickers');
sortSelect();
});
So the first line in the function does the moving of the <option>
tags and the second should call the sortSelect
function.
The trouble is I don't think the sortSelect
function is being called (there is nothing in the console log and the function should write to it). What's wrong with my code?