Salesforce : actionFunction example, call apex method from javascript

How to call Apex class method from javascript on Visulaforce page ? using actionFunction tag


In this following sample, we call apex method on click of a link and page load; and for the proff, we display name as changing text in page block.

public with sharing class AtestClass {
    public String name{get;set;}
    Integer count = 1;
    public void getCountDisplay(){
       
        name =  'you have clicked '+(count == 1? '***Started***' : '')+ (count++) +' times.';
    }
}



<apex:page controller="AtestClass">
    <apex:form >
        <a href="javascript:submitAfterTimeOver();">Click Me</a>
        <apex:actionFunction name="submitAfterTimeOver" action="{!getCountDisplay}" rerender="myBlock"/>
        <apex:pageBlock id="myBlock">{!name}</apex:pageBlock>
        <script>
            submitAfterTimeOver();
        </script>
    </apex:form>
</apex:page>

Comments

Popular Posts