Discussions

Ask a Question
Back to All

Edit Form Set Focus

Is it possible to set the focus of the first field in the form that people use to add/edit items in a template?

I tried adding the following to template.js, but it didn't run.

// the #field1  selector is wrapping the first field in the popup add/edit view
setFocusToFirstField('#field1');

function setFocusToFirstField(selector) {
  	console.log('I was called.');
    // Get the parent element using the selector
    var parentElement = document.querySelector(selector);
    
    if (parentElement) {
        // Find the first input, select, or textarea within the parent element
        var formField = parentElement.querySelector('input, select, textarea');
        
        // If a form field is found, set focus to it
        if (formField) {
            formField.focus();
        }
    }
}

I should add that I'm trying to do this in an older template that has all of that jpList stuf in it.