+ Start a Discussion

Call a javascript function from controller or call controller method from javascript

I am not sure what the subject should be, But here is what i want to do..


i am generating data for my flot graph  from my controller.

I have 2 <apex:selectList> in the VF page which captures project name and milestone. I need to pass these values into my controller method and that method returns a string value which would be used to build the graph. This graph needs to be generated on click of a button.


I have a html button on click of which the graph is generated.

How do i send the values of the selectlist into controller method?

Can i call the controller method from onclick of button and then run the rest of the javascript code to generate the graph?






Rajesh ShahRajesh Shah

I am not sure if I understand your question properly, so let me just say what is possible.

  1. You can call a controller function from JavaScript. For this, you need to use actionFunction
  2. You can also call a JavaScript function in some cases when a particular action is completed in controller. This can be done by using the oncomplete attribute of apex tags which invoked the event. Tags which support this are CommandButtion, actionSupport, actionPoller, etc.

I think using one of the above or a combination you can achieve your result.




You can call a controller method from the onclick method, but as soon as you make that call you have lost control of the page.  You can't carry on at that point, as the page has been submitted back.


As well as the options that Rajesh has given above, I've used the following method to execute javascript once the action method has completed:


(1) Create a new boolean controller property that defaults to false

(2) In the action method that sets up all the data, set the boolean property to true

(3) Embed the javascript inside two outputPanels.  The outer outputpanel is simply a container for the inner.  The inner outputpanel contains the javascript and is only rendered if the boolean method is set to true.