19

I have a lightning:input and when the focus is on it and you type the enter stroke: it refreshes the page.

I tried to prevent it by implementing the onchange: avoid the event to be fire but the callback is never launched.

Component.cmp:

<lightning:input
  type="email"
  onchange="{! c.callbackNeverCalled }"
/>

Controller.js:

callbackNeverCalled: function(component, event, helper) {
  event.preventDefault(); // Does not prevent from refreshing/redirecting the page & also 'deprecated'.

  event.getSource().getParams(); 
  // Is an empty object but the good way to do (cf. documentation)
}
7
  • Ok i checked the documentation of the lightning input and there is no onkeyup event. If you can add javascript to your page. Here's a solution I found. Jan 12, 2017 at 8:55
  • My callback for the onchange event is now working. But there are still two issues: event.preventDefault() do not prevent from refreshing/redirecting the page and I can access the keyup by doing event.getSource().getElement().KEYUP (this will be deprecated, displayed at usage for event.getSource().getParams() but this does not return anything other than an empty object at the moment).
    – Simonp
    Jan 12, 2017 at 18:01
  • event.KEYUP give us this message: Avoid relying on the native event attribute KEYUP as it is only included for backwards-compatibility. It will eventually be deprecated. Use the aura event API (e.g., getSource(), getParams(), etc) instead.
    – Simonp
    Jan 12, 2017 at 18:12
  • Still not fixed..
    – Simonp
    Jan 22, 2017 at 10:12
  • 1
    @SimonPlacentino Are you putting your lightning:input in a form? This is typically not how you'd write a component/app.
    – sfdcfox
    Feb 17, 2018 at 16:22

3 Answers 3

19

Thanks to SFDCFOX and PatMcClellan__c. Workaround is:

<span onkeypress="{!c.keyCheck}" class="slds-size--11-of-12">
    <lightning:input aura:id="body" label="" name="Body" placeholder="Enter message..." value="{!v.Message.Body__c}" />
</span>

keyCheck : function(component, event, helper){
    if (event.which == 13){
        helper.onSend(component, event);
    }    
}
4

In LWC this works as

<span onkeypress={keycheck}>
    <lightning-input label="input1" data-fieldapiname="Name">
    </lightning-input>
</span>

 keycheck(component, event, helper){
    if (component.which == 13){
        // call the js method i.e. this.handlerfilter();
    }
}
0

Try using the oncommit event of the lightning-input component. No need to use a wrapper span. It can be found in the documentation in the lightning component library.

2
  • 1
    commit fires when you press enter or when the input loses focus. In a type="search" input, it also fires when the user clicks the "x" to clear the search field. It does not include any params to tell you which occurred, so if you only want to take action when "enter" is pressed, it is not sufficient. Oct 14, 2021 at 14:00
  • @jasonClark agreed. Oct 16, 2021 at 3:31

You must log in to answer this question.

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