0

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?

Markup

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

Controller

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

                helper.handlerSearchEnterEvt(component,event,helper);
            }

        },true)
  }

1 Answer 1

2

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

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .