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
pradyprady 

Pass a <list> from apex into string array in javascript

Is it possible to pass a <list> from apex into javascript as array in VF page?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can't do this via a single line of code, as outputting a list on a page will simply render the string representation.

 

What you can do is use apex repeat tags to iterate the list and add to a  javascript array.

 

Something like:

 

 

var myArray = [];
var idx=1;
<apex:repeat value="{!myList}" var="ele">
myArray[idx++]="{!ele}";
</apex:repeat>

 

 

All Answers

bob_buzzardbob_buzzard

You can't do this via a single line of code, as outputting a list on a page will simply render the string representation.

 

What you can do is use apex repeat tags to iterate the list and add to a  javascript array.

 

Something like:

 

 

var myArray = [];
var idx=1;
<apex:repeat value="{!myList}" var="ele">
myArray[idx++]="{!ele}";
</apex:repeat>

 

 

This was selected as the best answer
Pradeep_NavatarPradeep_Navatar

You can assign a list as given below :

 

<script>

Var tempList=”{!myList}”;

alert(templist[0]);

</script>

Dust_In_The_WindDust_In_The_Wind

Pradeep_Navatar wrote:

You can assign a list as given below :

 

<script>

Var tempList=”{!myList}”;

alert(templist[0]);

</script>


This creates results in templist being an array of characters derived of every character across all myList entries.

 

The <apex:repeat> way works, but does anyone know how to incorporate this into a JS function that I can call again later?

 

ie

 

var tempArray = [ ];

function getSFList ( ) {

      // Put the Apex:Repeat here

}

bob_buzzardbob_buzzard

The apex:repeat will work in the same way inside a function as outside.  The rendered page will simply contain repeated assignment statements.

uHaveOptionsuHaveOptions
This is working great. How might I adapt it to pass a Map instead of a list? Say for example a map of an ID and a Name?