How to move up and down items in The listbox list box using JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.