Created
April 14, 2013 23:43
-
-
Save shobhitsharma/5384736 to your computer and use it in GitHub Desktop.
Javascript Listbox Filtering to rearrange the order of values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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