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
simhasimha 

how to pass a value to apex class from java script

Hi

    In a java script i have done some callculations and i  want to pass dat value to the apex class .

 

 for this i just used the apex function tag but how can i pass the parameter for a function?

 

this is my Visualforce code 

 

<script>

....

...

..

Age = CDateArr[3] - BDateArr[3];
document.getElementById('ageop').value = Age; 

agesend();

....

....

....

</script>

<apex:actionFunction name="agesend" action="{!age}"/>

 

 

 

apex code is

 

string ageval;

 ...

....

public void age(ages)
{
    this.ageval=ages;
}

 

if any one know the solution please help me

Best Answer chosen by Admin (Salesforce Developers) 
XactiumBenXactiumBen

You can do it in two ways - Put the Age value from javascript into an inputHidden apex tag that has a value from your controller, or add an apex param tag onto your actionFunction:

 

Using an inputHidden:

<script> Age = CDateArr[3] - BDateArr[3]; document.getElementById('{!$Component.ageop}').value = Age; </script> <apex:inputHidden id="ageop" value="{!ageVal}"> Apex: public String ageVal {get; set;}

 

Using an actionFunction:

 

 

<script> Age = CDateArr[3] - BDateArr[3]; agesend(Age); </script> <apex:actionFunction name="agesend" action="{!doAction}"> <apex:param name="age" value="0" assignTo="{!ageVal}" /> </apex:actionFunction> Apex: public String ageVal {get; set;} public void doAction() { // Do something here if required }

 

 

 

Using an actionFunction would be more beneficial if you wanted to run an action (It looks as if you don't need to)