Focus state bug on text field AJAX calls

Created on 3 December 2015, almost 9 years ago
Updated 16 October 2023, about 1 year ago

Problem/Motivation

#1824636: Do not move the cursor to the top of the page on ajax calls solved a form focus state issue where by default the focus state comes back to the form element after rebuilding part of the form after an AJAX call. This was fixed against a select element in the views module. The problem is that it works fine on select elements and checkboxes, but not on text fields and textareas. The default AJAX event for these text fields is 'blur', meaning AJAX triggers when unfocusing, for example by clicking anywhere else or pressing tab. The problem is that that by doing so, the focus comes back on these fields by default now, making it impossible to select any other form element in the form.

I don't know if this happens anywhere in Drupal out of the box, but I included a small module to reproduce the issue. Enable ajax_test and navigate to /admin/config/development/ajax-test to see the form.
A quick example of this behaviour is shown here. The top field has an AJAX callback (the sample module has it on both, resulting in a never ending back and forth focussing between the two fields). The focus always comes back to that field:

Proposed resolution

Return focus to the element that was focused prior to the execution of AJAX commands for event listeners attached to the blur event. This is needed because when the event listener is attached to blur event, user may have moved the focus to another element and then consequently triggering the blur event.

There could be some advanced use cases where the default behavior is not sufficient. For this reason, there's an API to explicitly override this if needed. For example, a custom form element could explicitly enable this behavior for itself.

🐛 Bug report
Status

Fixed

Version

10.2

Component
Ajax 

Last updated about 12 hours ago

Created by

🇨🇦Canada Danny_Joris

Live updates comments and jobs are added and updated live.
  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

  • Accessibility

    It affects the ability of people with disabilities or special needs (such as blindness or color-blindness) to use Drupal.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024