+ Start a Discussion
Dream_weaverDream_weaver 

How to call a controller method from javascript that will return a value not null..

Can we use----var x={!method} ?? in javascript...not working for me..

 

Using <action function >we can access controller method but the method is 'pagereferece' type so it will return null...I want to return a value from controller method to javascript.....How to do?

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SeAlVaSeAlVa

You have several options:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

 

you can also assign directly like you say "var x = {!methodA};" or "var x = '{!methodA}'; " (within quotes).

(take into account that you should have a method in apex called 'getMethodA' )

 

you can even reRender part of your code with javascript involved to reassign variables.

 

Hope this help. Regards

 

All Answers

SeAlVaSeAlVa

You have several options:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

 

you can also assign directly like you say "var x = {!methodA};" or "var x = '{!methodA}'; " (within quotes).

(take into account that you should have a method in apex called 'getMethodA' )

 

you can even reRender part of your code with javascript involved to reassign variables.

 

Hope this help. Regards

 

This was selected as the best answer
Raj.ax1558Raj.ax1558

You can use Actionfunction for calling method in cotroller -

 

suppose -

<apex: page>

<apex:form id ="frm">

<apex:actionfunction name="callfromJS"  action="{!controllerMethodName} reRender="frm"/>

 

</apex:form>

<script>

function JSmethodCallFromAnyAction()

{

callfromJS();

}

</apex:page>

 

 

This JS method you call from any action like onclick, onchange etc.  This method call actionfunction(actionfunction: used for calling controller method without submitting the page) . reRender is used for refresh particular protion.

 

 

Thank you.

 

Please marked as solution for benefits of others persons.

 

 

Dream_weaverDream_weaver

var x = '{!methodA}'; " (within quotes)---It worked..Thanks a lot..