In this example move up and move down the lists in list box.
Code:-
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <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>
- </head>
- <body>
- <select id="lst" size="15" multiple="">
- <option value="1">Merbin</option>
- <option value="2">Franklin</option>
- <option value="3">Jose</option>
- <option value="4">Geetha</option>
- <option value="5">Jino</option>
- <option value="6">Ganesh</option>
- <option value="7">Kumar</option>
- <option value="8">Reegan</option>
- <option value="9">Rajesh</option>
- </select>
- <br>
- <button onclick="listboxMove('lst', 'up');">Move Up</button>
- <button onclick="listboxMove('lst', 'down');">Move Down</button>
- </body>
- </html>
Move up and move down :- JavaScript
Reviewed by Network security
on
November 13, 2019
Rating:
No comments: