Focus on a form field after loading in JavaScript PDF Viewer

16 Oct 20251 minute to read

The JavaScript PDF Viewer library enables setting focus on a specific form field by using the focusFormField() API.

Follow these steps to bring a form field into focus during document load and on demand.

Step 1: Complete the configuration described in Getting started with the JavaScript PDF Viewer to build a working sample.

Step 2: Add the following markup and code to focus the desired form field when the document loads or when a button is clicked.

  <button id="focusFormField">FocusFormField</button>
//Event triggers while clicking the FocusFormField button.
document.getElementById('click').addEventListener('click', function () {
    var formField = viewer.retrieveFormFields();
    //API to bring the form fields in focus.
    viewer.focusFormField(formField[1]);
});
//Event triggers while loading the document.
viewer.documentLoad = (args) => {
    var formField = viewer.retrieveFormFields();
    //API to bring the form fields in focus.
    viewer.focusFormField(formField[1]);
};

View the sample on GitHub.