How to Focus a Form Field After Loading in JavaScript (ES5) PDF Viewer
14 Aug 20261 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('focusFormField').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]);
};