Yes, I know, this has already been answered, but I donot understand the answers or the methods.

I have a var of type STRING, say, myStr, I want to pass it to my controller method myMethod(). (it can also accept a string argument if needed).

CLARIFICATION : myMethod is a function in my CONTROLLER. JSCalltoController is the JS function that is called on clicking the button (THIS IS WITH ACTIONFUNCTION)

Sorry if there was any confusion.

What do I do?

Things I've tried :

1. Direct :

<button type="button" onclick="myMethod(myStr)">Enter</button>

The method is not called. I'm using system.debug to check whether the code reaches the function.

2. Actionfunction

<button type="button" onclick="JSCalltoController()">Enter</button>

<apex:actionFunction action="{!myMethod}" name="JSCalltoController">
    <apex:param name="myStr" assignTo="{!myStrController}" value="" />

The above, ofcourse, fails.

If someone could demonstrate how to use this, It'd be nice.

There is a distinct lack of adequate (and in simple enough language) documentation/examples for many Visualforce elements.

**UPDATE : ** whole page is here : http://codepad.org/baQewaaA part of controller is here : http://codepad.org/fJsEu6TJ.

I am very frustrated right now, hours of searching and still no results. Anyone who can help me understand exactly WHAT is going wrong here, would win my lifelong gratitude.

  • Your button should be calling JSCalltoController instead of myMethod. <button type="button" onclick="JSCalltoController (myStr);">Enter</button>
    – Prady
    Aug 29, 2016 at 11:36
  • but... what happens to apex:param? <apex:param name="NumberedString" assignTo="{!controllerValue}" value='myStr' />??
    – fraglord
    Aug 29, 2016 at 12:10
  • Since you are passing the value of mystr to JSCalltoController, the mystr value gets assigned to param and which inturn updates the controller property myStrController . I am assuming here that the you have defined a property called myStrController in your controller. Have you tried changing the button code to call the action function JSCalltoController ? If you are still having issues. Let us know what the issue is and we can help
    – Prady
    Aug 29, 2016 at 13:12
  • not working... it is not passing the var... at all... the recieving var in controller is empty/null
    – fraglord
    Aug 30, 2016 at 4:14
  • Can you post the code of the button, actionfunction, the method in controller.? You might not be passing the correct names. It is very difficult to debug the issue without all these.
    – Prady
    Aug 30, 2016 at 16:54

1 Answer 1


You need to call your action function method from your button

<button type="button" onclick="JSCalltoController(myStr);">Enter</button>

and your action function will be same

<apex:actionFunction action="{!myMethod}" name="JSCalltoController">
    <apex:param name="myStr" assignTo="{!YOURCONTROLLERPROPERTY}" value="" />

Apex Controller:

/*** Controller ***/
public class exampleCon {

public void setYOURCONTROLLERPROPERTY(String n) {


public PageReference myMethod() {
    return null;

  • hi, did you read my updated question? i clarified a couple of things there, thanks!
    – fraglord
    Aug 29, 2016 at 12:23
  • also, if i dont see how adding those lines to the controller would help...
    – fraglord
    Aug 29, 2016 at 12:27
  • does not work. value is not even being sent to controller... its blank/null
    – fraglord
    Aug 30, 2016 at 4:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .