Skip to content

Instantly share code, notes, and snippets.

@Sravanalaparthi
Last active March 16, 2016 02:02
Show Gist options
  • Save Sravanalaparthi/97e171a4025e6e271387 to your computer and use it in GitHub Desktop.
Save Sravanalaparthi/97e171a4025e6e271387 to your computer and use it in GitHub Desktop.
VisualForce: Passing Values from Javascript to Controller
<apex:page id="myPage" controller="InputfieldDemoController">
<!-- Include JQuery in the page -->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- JS function from which you want to pass the variable-->
<script>
function sendVariable(parameter){
jQuery('[id$=myHiddenValue]').val(parameter);
passValueToController();
console.log(jQuery('[id$=myHiddenValue]').val());
}
</script>
<apex:form>
<!-- Declare a hidden field and bind it to required variable in controller using value attribute -->
<apex:inputHidden value="{!valuefromJS}" id="myHiddenValue"/>
<!-- Action function for the rerendering the hidden variable-->
<apex:actionFunction name="passValueToController" action="{!someMethod}" rerender="myHiddenValue"/>
<!-- Command button to set the value -->
<apex:commandButton value="Set Value" onclick="sendVariable('new value'); return false;" />
</apex:form>
</apex:page>
public class InputfieldDemoController {
//Variable under test
public String valuefromJS {get; set;}
public pagereference someMethod(){
System.debug(valuefromJS);
return null;
}
}
<apex:page id="myPage" controller="InputfieldDemoController">
<!-- Include JQuery in the page -->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- JS function from as above but doing nothing other than writing the value to console to crosscheck-->
<script>
function sendVariable(parameter){
console.log(jQuery('[id$=myHiddenValue]').val());
}
</script>
<apex:form>
<!-- Declare a hidden field and bind it to required variable in controller using value attribute -->
<apex:inputHidden value="{!valuefromJS}" id="myHiddenValue"/>
<!-- Action function for the rerendering the hidden variable-->
<apex:actionFunction name="passValueToController" action="{!someMethod}" rerender="myHiddenValue">
<apex:param name="p1" value="" assignTo="{!valuefromJS}" />
</apex:actionFunction>
<!-- Command button to set the value -->
<apex:commandButton value="Set Value" onclick="passValueToController('new value'),sendVariable('parameter'); return false;" />
</apex:form>
</apex:page>
<apex:page id="myPage" controller="InputfieldDemoController">
<!-- Include JQuery in the page -->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- JS function to invoke controller method-->
<script type="text/javascript">
function sendVariable(parameter){
// Visualforce remote action manager is used to invoke the controller method synatax is
// Visualforce.remoting.Manager.invokeAction(parameters separated by comma goes here)
//
// Parameter 1:'{$RemoteAction.yourcontroller.yourmethod}' which is '{!$RemoteAction.InputfieldDemoController.someMethod}'
// in the example below
//
// Parameters 2: the parameters you want to pass to your method in controller.
// you can specify n number of parameters separated by comma
//
// Parameter 3: function(result, event){} in this result is the value or object returned by controller method if any
// and event object is used to know status of call and remote handling
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.InputfieldDemoController.someMethod}',
parameter,
function(result, event){
if (event.status) {
//do something here
console.log('Oh Ya');
}
},
{escape: true}
);
}
</script>
<apex:form >
<!-- Command button to set the value -->
<apex:commandButton value="Set Value" onclick="sendVariable('New Value'); return false;" />
</apex:form>
</apex:page>
Global class InputfieldDemoController {
//Variable under test
public static String valuefromJS {get; set;}
//Method you want to invoke in javascript should be annotated as below
@RemoteAction
global static void someMethod(String parameter) {
valuefromJS = parameter;
system.debug(valuefromJS);
}
}
@aravetic
Copy link

Hi,

valuefromJS value in outside someMethod is becoming null. I want this parameter value outside of remotefunction also. Please help me.

Thanks,
Charan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment