Play Games

Search This Blog

Wednesday, January 6, 2016

How to call apex method from Javascript in visualforce page

With the help of <apex:actionFunction> tag, we can directly call apex method from javascript.
Apex Class:
public class ClockWatch {
    public integer sec {get;set;}
    public ClockWatch() {
        sec=0;
    }
    public void increment() {
        sec=sec+1;
    }
}
Visualforce Page:
<apex:page controller="ClockWatch" id="page1">
    <script>
        function increment() {
            incrementTimer();
        }
    </script>
    <apex:form id="form1">
        <apex:actionfunction name="incrementTimer" action="{!increment}" />
        <apex:pageBlock id="block1">
            Integer I :   {!sec}<br/>
            <apex:commandButton value="Increment" onclick="increment();" rerender="block1"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Output:

No comments:

Post a Comment