You need to sign in to do that
Don't have an account?

Passing values from Javascript to Controller
Is there any to pass to parameters from a javascript to a actionFunction?
For eg I have a var 'x' in javascript set to some value. On some condition I need to call a actionFunction which calls a controller. I need to pass the value of x to the controller. I can pass using param tag of actionFunction to controller but how to pass x to the actionFunctio?
Using actionFunction is like any normal javascript function - just pass in parameters like you would normally.
<apex:actionFunction name="doStuff" action="{!doStuff}"> <apex:param name="x" value="x" assignTo="{!somevariable}" /> </apex:actionFunction> <script> function dosomejavascript() { // set up my param to pass to action function var myParam = 'abc'; /* call my action function - myParam variable should be set to 'somevariable' in my controller */ doStuff(myParam); } </script>
All Answers
Using actionFunction is like any normal javascript function - just pass in parameters like you would normally.
<apex:actionFunction name="doStuff" action="{!doStuff}"> <apex:param name="x" value="x" assignTo="{!somevariable}" /> </apex:actionFunction> <script> function dosomejavascript() { // set up my param to pass to action function var myParam = 'abc'; /* call my action function - myParam variable should be set to 'somevariable' in my controller */ doStuff(myParam); } </script>
I have similar problem but i don't solve, this is my easy code:
<apex:page standardController="opportunity" >
date {!(Opportunity.lastmodifieddate)}
<script src="/soap/ajax/10.0/connection.js" type="text/javascript">
</script>
<script type="text/javascript" src="/js/functions.js"></script>
<script>
[... some my code...]
var newvar1 = '1'
</script>
xxxxxxxxx
</apex:page>
Into the "xxxxxxxxx" I have to substitute a code which auto-save the value of "newvar1" into a field of opportunity when I open page of every opportunity
How can I do it ?
thanks
hey Ragjesh your code helped me a lot but am getting null value in my controller can you suggest me the exact solution of this problem
here is my code:
please Help me i am newbie in salesforce
Thanks so much. I was looking all over for a clean, easy-to-understand example of how to do this. You saved the day.
Hi all,
Can anyone tell me the exact way to pass a variable from js to controller, please?
javascript code:
var csvVals = "abc";
callToSetSelectedFields(csvVals);
vf code:
<apex:actionFunction name="callToSetSelectedFields" action="{!callToSetSelectedFields}">
<apex:param name="x" value="x" assignTo="{!RHSValuesForFields}" />
</apex:actionFunction>
controller:
public String RHSValuesForFields
{
get
{
RHSValuesForFields = csvVals;
return RHSValuesForFields;
}
set;
}
public String callToSetSelectedFields()
{
//RHSValuesForFields = csvFieldVals;
return RHSValuesForFields;
}
This code is giving error for unknown csvFieldVals
How should i use the csvFieldVals in controller?
In your controller, set up the property the parameter binds to like this:
public String RHSValuesForFields { get; set; }
callToSetSelectedFields should be able to get the value out of that.
Thanks Joe.
But after doing that i get null value in the function.
The work around i used was adding a hidden field and setting its value in javascript then using that fields value in controller.
Hi Dips,
Can you please post your code snippet if possible. (with the work around)
I am facing a similar problem. It is a bit urgent.
Thank you in advance.
Regards,
Lakshmi.
laks,
The page contains <apex:inputHidden id="hiddenSecId" value="{!RHSValsForFields}"/>
Then i set the field value from javascript as:
var hiddenSec = document.getElementById(hiddenSecId);
document.getElementById(hiddenSecId).value = totalValsSelected;
This way the 'RHSValsForFields' field value is set from javascript and can be used in controller now.
Hope this solves your problem.
Hi Dips,
Thank you for the reply.
I figured it out and did it with a combination of hidden fields, action function and javascript.
Regards,
Lakshmi.
Thank you,
Shohrat