This article describes how to use JavaScript to control the move up and down of listbox lists. The example shows how to use javascript to operate listbox lists, for more information about how to use JavaScript to control the move up and down of items in the listbox list box, see the following example. Share it with you for your reference. The specific analysis is as follows:
This JS Code can control the movement of elements in the listbox up or down. This function is very useful. The detailed code is as follows:
The Code is as follows:
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;
}
//..
//..
Listbox_move ('countrylist', 'up'); // move up the selected option
Listbox_move ('countrylist', 'low'); // move down the selected option
The following is a detailed DEMO code, which can be used in a browser.
The Code is as follows:
Click below buttons to select or deselect all options from select box.
India United States China Italy Germany Canada France United Kingdom
Move Up
Move Down
Script
Function listboxMove (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;
}
Script
I hope this article will help you design javascript programs.