function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rzsrzs 

Passing a select list to a Controller from Javascript using apex:actionFunction

Hello,

 

I want to pass all the values present in a select list with id my_select_list, to a Controller from Javascript.Im using apex:actionFunction as follows:

 

In my Page Editor:

 

for (var i = 0; i < document.getElementById('my_select_list').length; i++)
{
                    methodOneInJavascript(document.getElementById('my_select_list').options[i].value);
                  
                  
}


<apex:outputText title="Selected Values" value="Selected Values are: {!selectedValues}" id="showstate" />

<apex:actionFunction name="methodOneInJavascript" rerender="showstate" >
            <apex:param name="firstParam" assignTo="{!selectedValues}" value="" />
 </apex:actionFunction>




In my controller:

 

 

Integer ctr = 0;
String[] selectedValues = new String[1000];


 public String[] getSelectedValues()
{
        return selectedValues;
 }
 
public void setselectedValues(String str) { selectedValues[ctr] = str; ctr+=1; }

 

But,irrespective of how many items were in the select list, the value of ctr always stays 0.Also,outputText always shows the last value passed to the Controller.

Am i going wrong somewhere ?

Is there a way to pass the entire select list to the Controller from Javascript in one shot using apex:actionFunction , and the store the select list options in a List object ?

Some sample code would be really appreciated.

 

Thank You.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

As soon as the first call to methodOneInJavascript takes place, the form gets submitted back to the controller and your javascript is over.

 

In your javascript I'd suggest iterating the options and converting them to a comma separated string.  Then pass that as the single parameter.  Then in setSelectedValues, split the string back into an array.