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
Apoorva SharmaApoorva Sharma 

Using values from javascript in apex class

Hi,
i have a requirement in which i need to get  javascript variables in apex class.If anyone can help with a code example, I'd appreciate it.
Best Answer chosen by Apoorva Sharma
Roy LuoRoy Luo
Assuming you have a controller, a visualForce page with some javascript. You may use an apex inputHidden control for controller data binding, then at the client side, just find the hidden field by Id to get or set the value.
 
public class MyController
 {
 	public MyController(ApexPages.StandardController controller)
 	{

 	}
 	public String ParamValueFromJavaScript {get;set;}
 }
 
<apex:page standardController="YourCustomObject__c" extensions="MyController" id="page">
     <apex:inputHidden id="hiddenParamValueFromJavaScript" value="{!ParamValueFromJavaScript}" />
    
    <apex:dynamicComponent componentValue="{!form}" id="dynamic"/>
    <script type="text/javascript">
       var ctrl = document.querySelector('[id$="hiddenParamValueFromJavaScript"]');
       ctrl.value = "Here is the value from JavaScript code";
    </script>
</apex:page>