How to call JAVASCRIPT method on Enter key press in Salesforce Lightning component?

Sample Code:

Component:

  1. <aura:component implements="force:appHostable" >  
  2.       
  3.     <aura:attribute name="strText" type="String"/>  
  4.       
  5.    <div class="slds-box slds-theme_default">  
  6.   
  7.        <span onkeypress="{!c.callKeyUp}">  
  8.              
  9.            <lightning:input label="Enter Text"  
  10.                             value="{!v.strText}" />  
  11.              
  12.        </span>  
  13.           
  14.     </div>  
  15.       
  16. </aura:component>  

Component Controller:

  1. ({  
  2.   
  3.     callKeyUp : function(component, event, helper) {  
  4.           
  5.         if ( event.keyCode == 13 )  
  6.             alert(component.get("v.strText"));  
  7.           
  8.     }  
  9.       
  10. })  

Output:

Enter some text and click Enter button.


No comments:

Post a Comment