Avatar of ksedran
ksedran
 asked on

Get selected items from multi-select listbox with Javascript

Hi All,

I have a multi-select listbox in a pop-up window that is populated with items from Active Directory.

On the CloseWindow event I want to load all the selected items into a textbox on the parent form.

It's working fine with the code below. But the problem is it is extremely slow because the listbox contains over 10,000 items and I am iterating over each item to determine if it is selected or not.

Is there a way to get the selected items without having to iterate through every item like I am doing here?

Thanks!
function CloseWindow(){
var i = 0;
var selectedItems;
selectedItems = "";
var selectedObj = document.getElementById('r_SecurityGroupList');
                
if(selectedObj){
for(i=0; i<document.getElementById('r_SecurityGroupList').options.length; i++)
  {
    if(document.getElementById('r_SecurityGroupList').options[i].selected){
      selectedItems += document.getElementById('r_SecurityGroupList').options[i].value + "; ";}}
 
selectedItems = selectedItems.slice(0,-1);
window.opener.document.getElementById('r_SecurityGroup').value += selectedItems;
}
window.close();
return false;
}

Open in new window

JavaScript

Avatar of undefined
Last Comment
ksedran

8/22/2022 - Mon
Maverick_Cool

to make fast the solution would keep track of selected value in global variable. when a user selects item in that, while changing data will be stored
and
when you need its there in a variable, you attach event to run javascript onselect or onchange event
ASKER CERTIFIED SOLUTION
Maverick_Cool

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ksedran

ASKER
Hey Maverick_Cool,

Can you post an example of how to get the selected values using the innerhtml and Regexp?

Thanks.
ksedran

ASKER
Hey Maverick_Cool,

Can you post an example of how to get the selected values using the innerhtml and Regexp?

Here is some text from the innerhtml property:
<OPTION value=\"ASL Gas Brokers\" selected>ASL Gas Brokers</OPTION>
<OPTION value=BillingContact selected>BillingContact</OPTION>
<OPTION value=TechnicalContact selected>TechnicalContact</OPTION>
<OPTION value=\"Change Management\" selected>Change Management</OPTION>

Thanks.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23