I am using lightning-record-edit-form to display to users custom Opportunity form with the help of lightning-input-field. However, I am unable to set focus on a particular field.

I have a 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 on Amount field using the below code, it didn't work.

  let fieldToFocus = this.template.querySelector("lightning-input-field[data-fieldname='Amount']");

Has anybody been able to make focus work with lightning-input-field?


2 Answers 2


Unfortunately lightning-input-field doesn't have @api method focus. You can use lightning-input instead.


Recent Updates:

Salesforce seems to have recently updated the global methods for lightning-input-field and snuck in a focus method, alongside the reset method that's been there: Input Field Specification

Updated Methods for lightning-input-field includes focus.

Sample code


    <lightning-input-field field-name="Amount" data-fieldname="Amount"></lightning-input-field>


Be sure to fire the focus() method when all rendering is completed, or asynchronously (setTimeout method with no delay) to ensure there are no errors on the method call.

let elem = this.template.querySelector("lightning-input-field[data-fieldname='Amount']");
setTimeout(() => elem.focus());


The focus() method does not work currently in the LWC Local Web Development Server. You'll need to deploy your code to an org to verify.

You must log in to answer this question.

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