Sunday, 5 February 2017

How to call apex method on press of Enter Key in Salesforce?

Sample Code Snippet:

<apex:page>
  <apex:form>
<apex:inputText id="zipCode" value="{!zipCodeFromSearch}"
                           onkeypress="return pressEnter(event)">
       <apex:actionFunction action="{!searchStoreOnAddress}" name="enterFunc"/>
    </apex:inputText>
</apex:form>
<script type='text/javascript'>
function pressEnter(ev)  {
if (window.event && window.event.keyCode == 13 || ev.which == 13) {
enterFunc();
return false;
} else {
return true;
}
}
</script>

</apex:page>


  • searchStoreOnAddress is the apex method
  • When User press Enter Key in Text Box then Javascript function-pressEnter(event)  is executed which calls action function(by enterFunc()) and then actionFunction executes searchStoreOnAddress controller method.