So I want to be able to set the focus on a lighting:input searchbar as soon as a tab is clicked, but I can't figure out how to target it. In javascript I could just do document.getElementById("someId").focus(); but lightning:input components don't have an id parameter. Any ideas?

closed as off-topic by glls, Ratan Paul, Martin Lezer, Raul, Himanshu Oct 27 '17 at 12:35


  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – glls, Ratan Paul, Martin Lezer, Raul, Himanshu


In order to set the focus you will need to access the component using it's id -

You can retrieve a component using its local ID in your JavaScript code. Create a local ID by using the aura:id attribute. For example:

<lightning:button aura:id="button1" label="button1"/>

Find the button component by calling cmp.find("button1") in your client-side controller, where cmp is a reference to the component containing the button.

Note: This example uses a button but you can do it on input field as well and you need to do that in init which is page initiation.

Source - https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_ids.htm

  • Please make sure you include the essentials of the links you posted, just in case they no longer become available. Thanks – glls Oct 26 '17 at 0:28
  • @Bend'Straw did this solve your problem? I was not able to focus on ligthning:input, how did you solve it? I'm getting this error message Cannot read property 'focus' of null – Basti Nov 29 '17 at 11:51
