Place focus on error block on page reload

This commit is contained in:
Corinne Durrmeyer 2024-10-04 10:26:18 +02:00
parent fbb7bd7989
commit 8ca1f82b01
No known key found for this signature in database
GPG key ID: DDC049DDA35585B6
2 changed files with 7 additions and 4 deletions

View file

@ -2,8 +2,10 @@ import { Controller } from '@hotwired/stimulus';
export class AutofocusController extends Controller {
connect() {
const element = this.element as HTMLInputElement;
const element = this.element as HTMLInputElement | HTMLElement;
element.focus();
element.setSelectionRange(0, element.value.length);
if ('value' in element) {
element.setSelectionRange(0, element.value.length);
}
}
}