JavaScript controls two listbox lists to exchange data between the left and right _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to control the exchange of data between the left and right sides of two listbox lists in JavaScript. The example shows how to operate listbox in javascript, which is of great practical value, for more information about how to use JavaScript to control the exchange of data between two listbox lists, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

This function is often used. You can move the elements in the list box on the left to the right, or move the elements in the list box on the right to the left.

The Code is as follows:

Function listbox_moveacross (sourceID, destID ){
Var src = document. getElementById (sourceID );
Var dest = document. getElementById (destID );
For (var count = 0; count <src. options. length; count ++ ){
If (src. options [count]. selected = true ){
Var option = src. options [count];
Var newOption = document. createElement ("option ");
NewOption. value = option. value;
NewOption. text = option. text;
NewOption. selected = true;
Try {
Dest. add (newOption, null); // Standard
Src. remove (count, null );
} Catch (error ){
Dest. add (newOption); // IE only
Src. remove (count );
}
Count --;
}
}
}
//..
Listbox_moveincluss ('countrylist', 'selectedcountrylist ');


The following is the Demo code, which can be executed directly in the browser.

The Code is as follows:

Click below buttons to move selected options right or left.








Afghanistan Bahamas Barbados Belgium Bhutan China Croatia Denmark France

>

<

Afghanistan Bahamas Barbados Belgium Bhutan China Croatia Denmark France

Script
Function listboxMoveacross (sourceID, destID ){
Var src = document. getElementById (sourceID );
Var dest = document. getElementById (destID );
For (var count = 0; count <src. options. length; count ++ ){
If (src. options [count]. selected = true ){
Var option = src. options [count];
Var newOption = document. createElement ("option ");
NewOption. value = option. value;
NewOption. text = option. text;
NewOption. selected = true;
Try {
Dest. add (newOption, null); // Standard
Src. remove (count, null );
} Catch (error ){
Dest. add (newOption); // IE only
Src. remove (count );
}
Count --;
}
}
}
Script

I hope this article will help you design javascript programs.

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.