JavaScript controls the items in the listbox list box to move up and down

From , 3 Years ago, written in JavaScript, viewed 232 times.
URL https://pastebin.vip/view/c61aed64
  1. function listbox_move(listID, direction) {
  2.  
  3.     var listbox = document.getElementById(listID);
  4.     var selIndex = listbox.selectedIndex;
  5.  
  6.     if(-1 == selIndex) {
  7.         alert("Please select an option to move.");
  8.         return;
  9.     }
  10.  
  11.     var increment = -1;
  12.     if(direction == 'up')
  13.         increment = -1;
  14.     else
  15.         increment = 1;
  16.  
  17.     if((selIndex + increment) < 0 ||
  18.         (selIndex + increment) > (listbox.options.length-1)) {
  19.         return;
  20.     }
  21.  
  22.     var selValue = listbox.options[selIndex].value;
  23.     var selText = listbox.options[selIndex].text;
  24.     listbox.options[selIndex].value = listbox.options[selIndex + increment].value
  25.     listbox.options[selIndex].text = listbox.options[selIndex + increment].text
  26.  
  27.     listbox.options[selIndex + increment].value = selValue;
  28.     listbox.options[selIndex + increment].text = selText;
  29.  
  30.     listbox.selectedIndex = selIndex + increment;
  31. }
  32. //..
  33. //..
  34.  
  35. listbox_move('countryList', 'up'); //move up the selected option
  36. listbox_move('countryList', 'down'); //move down the selected option
  37.  
  38.  
  39. //javascript/8787

Reply to "JavaScript controls the items in the listbox list box to move up and down"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website