0

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="" />
</apex:actionFunction>

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 '16 at 11:36
  • but... what happens to apex:param? <apex:param name="NumberedString" assignTo="{!controllerValue}" value='myStr' />?? – fraglord Aug 29 '16 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 '16 at 13:12
  • not working... it is not passing the var... at all... the recieving var in controller is empty/null – fraglord Aug 30 '16 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 '16 at 16:54
2

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:actionFunction>

Apex Controller:

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

public void setYOURCONTROLLERPROPERTY(String n) {
    YOURCONTROLLERPROPERTY = n;
}

public String getYOURCONTROLLERPROPERTY() {
    return YOURCONTROLLERPROPERTY;
}

public PageReference myMethod() {
    return null;
}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.