[SalesForce] Accessing a map with dynamic key (a javascript string valiable )

I am in a situation where i have a map in my Apex code and i want to access its data with a key that i am passing through my javascript function. I tried with a couple of things that include directly putting js variable as a key but it gives error :

Error: Unknown property

I also tried with assigning the value of javascript variable to visualforce variable within my javascript if else confitions. but that doesnt work too.. here is my code snippit.

    if( strName == 'ABC' ){
            alert(strSurveyName);
            <apex:variable var="keySurveyName" value = "ABC" />
    }else if( strName == 'XYZ' ){
            alert(strSurveyName);
            <apex:variable var="keySurveyName" value = "XYZ" />
    }

console.log("{!keySurveyName}");

<apex:repeat value="{!StatusObjMap[keySurveyName]}" var="temp">
.....
.....
.....

In both the conditions it displays XYZ in output for the last debug statement. Any help would be highly appriciated.

Best Answer

You can't use a javascript variable as a visualforce key to an apex map, because the Visualforce is evaluated server side, then rendered down to the browser where the javascript runs. The Visualforce renderer does not know about or understand your javascript variables.

You will need to think of a different way of solving your problem.

EDIT: which is what @KeithC said in his comment...

Related Topic