I have a input field in lightning component and I'm struggling with two requirements. Autofocus - after page is loaded and on Enter key I want to trigger function.

I can handle those requirement separately, but
1) <lightning:input name="xxx" label="xxx" aura:id="searchInput" onchange="{!c.change}"/>

change: function(component, event, helper){
        alert(event.getParams().keyCode);
    }

is always undefined

2) <ui:inputText aura:id="searchInput" ... is not working with

setTimeout(function(){ 
            component.find("searchInput").focus();
        }, 100);

I know that lightning:input is still in beta, but do you have any idea how to solve both requirements?

Find the logic below to trigger function on "ENTER" key

<ui:inputText class="form-control" aura:Id="todoText" required="true" value="" keypress="{!c.onHitEtter}" updateOn="keypress"/>

In Controller:

onHitEtter : function(component,event) {

        if(event.getParams().keyCode === 13) {
            //your logic here;
        }
    }
  • thanks, but I have the code there. I also need automatic focus on that input field, when the page is loaded (I have 2 requirements) – Karel L Nov 21 '17 at 7:10

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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