Transferring options from one multiple selection boxes to other
We can transfer options from one selection box to other by selecting one by one or at one go. The uses of such a type of selection are you can select more than one options and it offers a better picture than selecting a group of checkboxes. To get an idea how the script works, see the demo at the end of this page. You can download the page with this code also.
This tutorial explains on how to move elements from one list to other, there are one more tutorial where the second list options are dynamically added or generated based on the selection of the first list.
You can see there are four main functions the page does and each is connected to one button. All the buttons have onlick event handler connected to one function. The page on load calls one function adoption_all_list() through the body tag to populate the first drop down box with default values. There are tutorials on adding elements or options to a list box to know how the function works. There is another tutorial on removing options from the list box. You must read these two tutorials before reading this. So out of the four buttons we will discuss the button which moves the selected options from first list box to second list box. For other functions refer to those adding and removing option tutorials. The function we will discuss is addOption_list(), it moves the selected options to the second list box. On execution of this function it collects the elements selected, to do this a for loop is used which loops through all the options of the first list box. The line
> does that. While inside the loop we can get the status of any element by checking selected event and it returns true if the element is selected. Then we can use if condition to get the status.
So if it is selected then we have to execute two steps ( inside the above if condition ) first we will add the option to second drop down list and then remove it from first list. Here are the two steps.
The first step uses addOption function to insert the option and the second step uses removeOption function to remove that element from fist list box.
Here is the full function code for moving elements from one drop down to second drop down.
Here is the html code to display the drop down boxes and the buttons.
Subscribe to our YouTube Channel here
This article is written by plus2net.com team.