Improve the code to get the selected items from select tag

Below is the code which i am using to select a item from one select tag and adding it a another select tag. Now the problem is the my first select tag contains over 10000 items. And my code checks whether the each item is selected in select tag one and if selected then checks whether the same item exist in the second select tag. This is very inefficient since it takes a lot of time do so. Can any one suggest a better code by which i can directly get the multiple selected items from the select tag one and then i only have to check whether they exist in second select tag and add them.

if(Name.selectedIndex >= 0)
            {
                for(i=0;i<Name.options.length;i++)
                {

                    if(Name.options[i].selected)
                    {

                        exist   = false
                        for(j=0;j<addedScrips.options.length;j++)
                        {

                            if(addedScrips.options[j].value == Name.options[i].value)
                            {
                                exist   = true;
                                break;

                            }
                        }
                        if(exist == false)
                        {
                            optVal =    Name.options[i].value;
                            optText = Name.options[i].text;
                            if(optVal == "NoMatch")
                            {}
                            else
                            {
                                addedScrips.options[addedScrips.options.length] =   new Option(optText,optVal);
                              break;

                            }
                        }
                    }
                }
                //sortOptions(addedScrips)
            }

Answers


If you use

var selectedValues = $('#multipleSelect').val();

on a multi-select list, you will get an array of all the selected values. Then use the array to filter and then add the missing values to your second list.

Hope this helped.

PS: Having a select with 10.000 values is a pain and not exactly user friendly ... i would suggest you to use Chosen which has a nice search option for the select/multi-selects.


Need Your Help

In need of JPEG test images with exif thumbnaill in none-compressed TIFF format

testing jpeg thumbnails exif

I know most (>>99%?) JPEG thumbnails embedded in APP1 EXIF are JPEG compressed (Still in TIFF format). But there is the possibility of a none-compressed TIFF format thumbnail. The problem is I just

TextView Color in the Spinner

android android-textview android-spinner

What i want is the Text color of the spinner should be white at runtime when it is displayed inside the Spinner as it is in spinner 1 and 2 and the text color of the list of Spinner should be black...