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.