Skip to content

Instantly share code, notes, and snippets.

@shobhitsharma
Created April 14, 2013 23:43
Show Gist options
  • Save shobhitsharma/5384736 to your computer and use it in GitHub Desktop.
Save shobhitsharma/5384736 to your computer and use it in GitHub Desktop.
Javascript Listbox Filtering to rearrange the order of values
function listbox_move(listID, direction) {
var listbox = document.getElementById(listID);
var selIndex = listbox.selectedIndex;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.selectedIndex = selIndex + increment;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment