Move up and move down :- JavaScript



In this example move up and move down the lists in list box.

Code:-

  1. <!DOCTYPE html> 
  2. <html> 
  3.  
  4. <head> 
  5.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  6.  
  7.  <script> 
  8.   function listboxMove(listID, direction) 
  9.   { 
  10.    var listbox = document.getElementById(listID); 
  11.    var selIndex = listbox.selectedIndex; 
  12.    if (-1 == selIndex) 
  13.    { 
  14.     alert("Please select an option to move."); 
  15.     return; 
  16.    } 
  17.    var increment = -1; 
  18.    if (direction == 'up') 
  19.     increment = -1; 
  20.    else 
  21.     increment = 1; 
  22.    if ((selIndex + increment) < 0 || 
  23.     (selIndex + increment) > (listbox.options.length - 1)) 
  24.    { 
  25.     return; 
  26.    } 
  27.    var selValue = listbox.options[selIndex].value; 
  28.    var selText = listbox.options[selIndex].text; 
  29.    listbox.options[selIndex].value = listbox.options[selIndex + increment].value 
  30.    listbox.options[selIndex].text = listbox.options[selIndex + increment].text 
  31.    listbox.options[selIndex + increment].value = selValue; 
  32.    listbox.options[selIndex + increment].text = selText; 
  33.    listbox.selectedIndex = selIndex + increment; 
  34.   } 
  35.  </script> 
  36.  
  37. </head> 
  38.  
  39. <body> 
  40.  
  41.  <select id="lst" size="15" multiple=""> 
  42.   <option value="1">Merbin</option> 
  43.   <option value="2">Franklin</option> 
  44.   <option value="3">Jose</option> 
  45.   <option value="4">Geetha</option> 
  46.   <option value="5">Jino</option> 
  47.   <option value="6">Ganesh</option> 
  48.   <option value="7">Kumar</option> 
  49.   <option value="8">Reegan</option> 
  50.   <option value="9">Rajesh</option> 
  51.  </select> 
  52.  <br> 
  53.  <button onclick="listboxMove('lst', 'up');">Move Up</button> 
  54.  <button onclick="listboxMove('lst', 'down');">Move Down</button> 
  55.  
  56. </body> 
  57.  
  58. </html>
Move up and move down :- JavaScript Move up and move down :- JavaScript Reviewed by Network security on November 13, 2019 Rating: 5

No comments:

Useful Information

Powered by Blogger.