I have a <lightning:inputField aura:id="inputField1"> in a modal window, and I want to focus on this field when the modal opens, so that the user doesn't have to use their mouse or tab to set focus.

However, when I try to set focus like this: cmp.find("inputField1").focus(), it throws an error saying that focus() is not a function. This does work, however, with lightning:input aura:id="inputField1".

I assume this is how Salesforce is using the Locker (or Proxy) to create elements, and it's not giving me a reference to the actual input element (maybe for security concerns in Communities or Portals).

Can anyone think of a way to set focus on this field?

  • are there 2 components in your app with same id="inputField" ? – Pranay Jaiswal Dec 6 at 21:02
  • also can you check whats the value of console.log(Array.isArray(cmp.find("inputField"))); – Pranay Jaiswal Dec 6 at 21:08
  • No, it's a singular element, the id is not actually "inputField" but descriptive of the application. The isArray function therefore returns false. – Jon Duelfer Dec 6 at 21:16
  • can you add some more code that will help us reproduce this issue? – Pranay Jaiswal Dec 6 at 21:18
  • No, but I think it's pretty descriptive. If you add a <lightning:inputField> element and then in your javascript controller try to call, cmp.find("you_element").focus(), you will get an error (or so I believe). – Jon Duelfer Dec 6 at 21:21
up vote 3 down vote accepted

lightning:inputField does not support focus as per their official documentation.

src: https://developer.salesforce.com/docs/component-library/bundle/lightning:inputField/specification

  • You used <lightning:input>, not <lightning:inputField>, which I'm assuming gets generated different from the Lightning perspective. As in my question, <lightning:input> seems to work fine. – Jon Duelfer Dec 6 at 21:29
  • Mybad, I got confused between the two. – Pranay Jaiswal Dec 6 at 21:33
  • Ah yeah I see the documentation for input:field lists the function as supported. Nice find. – Jon Duelfer Dec 6 at 21:36

Your Answer

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.