I have a listbox which has two buttons to control the priority of the contained items. Everything is working except for the move down logic when multiple items are selected.
If two items are selected, nothing happens, and if three or more are selected, only the bottom one moves.. is there is a simple way to handle this using jQuery?
Current implementation:
<div id="selected">
<div><strong>Selected</strong></div>
<div id="selected-items">
<input type="select" multiple="multiple" id="selected-items-select">
<option/>
</input>
</div>
</div>
<div id="priority" style="display: none;">
<div><input type="button" id="move-up" value="^" /></div>
<div><input type="button" id="move-down" value="v" /></div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#move-up').click(moveUp);
$('#move-down').click(moveDown);
});
function moveUp() {
$('#selected-items select :selected').each(function (i, selected) {
$(this).insertBefore($(this).prev());
});
$('#selected-items select').focus().blur();
}
function moveDown() {
$('#selected-items select :selected').each(function (i, selected) {
$(this).insertAfter($(this).next());
});
$('#selected-items select').focus().blur();
}
</script>
Also, notice the $('#selected-items select').focus().blur();
line.. I have it because sometimes the priorities don't update until the listbox gets focus. Is there a cleaner way to do this too?