You need to sign in to do that
Don't have an account?
how to pass javascript function with arguments to controller..
I want to pass the javascript function's arguments to controller through actionFunction but I am not sure if I am doing it right...here is the visual force code snippet that I am working..
<apex:form >
<apex:actionFunction name="populateFields" action="{!populateFields}" rerender="Email" status="Estatus"/>
<apex:param name="ename" value="{!custObj.Employee_Email__c}" assignTo="{!populateFields}" />
<apex:pageBlock mode="edit" id="thePageBlock">
<apex:actionRegion >
<apex:pageBlockSection columns="1">
<apex:inputField value="{!custObj.Employee_Name__c}" onselect="populateFields" id="name"/>
</apex:pageBlockSection>
....
.......
</form>
and in controller
public Contact populateFields(String empName) {
// I want to get the inputField value from Visual force page here when the onselect event happens
}
I'm not sure exactly what you're trying to do. But the best way to pass things from a Visualforce page to a controller when javascript is involved is the use of the hiddenfields.
Example:
public String SomeSring {get;set;} public void SomeFunction() { //Do something with the string }
Visualforce Page:
<apex:page controller="TestForBlog"> <apex:form> <script> function populateFieldsjs() { document.getElementById('{!$Component.hiddenfieldid}').value = document.getElementById('{!$Component.name}').value; ActionFunctionAjax(); } </script> <apex:actionFunction id="ActionFunctionAjax" action="{!SomeFunction}" reRender="ReRenderMe"/> <apex:outputPanel id="ReRenderMe"> <apex:inputHidden value="{!SomeString}" id="hiddenfieldid"/> <apex:inputField value="{!custObj.Employee_Name__c}" id="name"/> <a href="#" onclick="populateFieldsjs()">Run Javascript</a><apex:outputPanel> </apex:form> </apex:page>
I just wrote this very quickly so be careful when copying/pasting as something might be off by a character or so. But the basic flow is there.
In this case you'd fill in a value, click "Run Javascript" it then copies the input text value to the hidden field, then calls the actionfunction which rerenders the output panel which stores the values of the hidden text to the controller.
All Answers
try with JS Script Tag :
<script>
function populateFields(value){
actionpopulateFields(value);
}
</script>
<apex:form >
<apex:actionFunction name="actionpopulateFields" action="{!populateFields}" rerender="Email" status="Estatus"/>
<apexaram name="ename" value="{!custObj.Employee_Email__c}" assignTo="{!populateFields}" />
<apexageBlock mode="edit" id="thePageBlock">
<apex:actionRegion >
<apexageBlockSection columns="1">
<apex:inputField value="{!custObj.Employee_Name__c}" onselect="populateFields('{!custObj.Employee_Name__c}')" id="name"/>
</apexageBlockSection>
....
.......
</form>
I'm not sure exactly what you're trying to do. But the best way to pass things from a Visualforce page to a controller when javascript is involved is the use of the hiddenfields.
Example:
public String SomeSring {get;set;} public void SomeFunction() { //Do something with the string }
Visualforce Page:
<apex:page controller="TestForBlog"> <apex:form> <script> function populateFieldsjs() { document.getElementById('{!$Component.hiddenfieldid}').value = document.getElementById('{!$Component.name}').value; ActionFunctionAjax(); } </script> <apex:actionFunction id="ActionFunctionAjax" action="{!SomeFunction}" reRender="ReRenderMe"/> <apex:outputPanel id="ReRenderMe"> <apex:inputHidden value="{!SomeString}" id="hiddenfieldid"/> <apex:inputField value="{!custObj.Employee_Name__c}" id="name"/> <a href="#" onclick="populateFieldsjs()">Run Javascript</a><apex:outputPanel> </apex:form> </apex:page>
I just wrote this very quickly so be careful when copying/pasting as something might be off by a character or so. But the basic flow is there.
In this case you'd fill in a value, click "Run Javascript" it then copies the input text value to the hidden field, then calls the actionfunction which rerenders the output panel which stores the values of the hidden text to the controller.
Thanks much, bbrantly! It works!! I dont have to even assign the value
to the name from the hidden field...the hidden field value is getting automatically
set from the selected name..I dont understand yet how that is happening though.
May be the field is being passed to the standard controllers record in some way?!
I have tried almost all permutations and combinations with the <apex:param>..and assigning
values to function through <apex:param>..it just doesnt work for me..but thanks
for the reply kodiyakirti.