Currently I am using an <lightning:input> with an onkeypress listener.

Everything is ok, but it applies whole component. For example I press 'enter'. I didn't write this code but it execute this function.

Moreover, when I typed nothing on <lightning:input> but just press 'enter' it causes an error on the other component:

[Cannot read property 'setParams' of undefined]

I just want to apply current component 'enter' key search, not in all components. What am I doing wrong? How can I fix it?


<lightning:input aura:id="dptCode" onchange="{!c.handleBlur}" updateOn="keyup" onkeypress="{!c.keyPressed}" maxlength="20"/>


keyPressed : function(component,event,helper){
    document.addEventListener('keypress', function(e){



Hi you need to add a null check like below, So that you will only call helper method when you have some text on input.

    if (e.keyCode == 13) {
    var searchTerm = component.find("dptCode");
      if (!$A.util.isEmpty(searchTerm)) {
          helper.handlerSearchEnterEvt(component, event, helper);

Best Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.