Choices icon indicating copy to clipboard operation
Choices copied to clipboard

Preserve autofocus attribute or add a shouldFocus option

Open adrienpoly opened this issue 4 years ago • 2 comments

Is your feature request related to a problem? Please describe. Unless I am mistaken when an input field has an autofocus attribute, Choices does not autofocus to this field. Probably because the autofocus remains on the hidden field

Describe the solution you'd like I see two options

  1. detect the autofocus attribute and forward it to the choices input
  2. add a new option shouldFocus

Describe alternatives you've considered Currently, I have a workaround using a callback in callbackOnInit

callbackOnInit: function () {
    this.input.focus()
}

This is a simpler version of what I am using since typically I can have multiple fields on my page with only one being autofocused. So there is a need for additional logic. This is where having this as an option or directly compatible with the autofocus attribute would be very nice

adrienpoly avatar Apr 17 '20 06:04 adrienpoly

It would be nice feature. Thumbs up!

mysticman avatar Jun 12 '20 08:06 mysticman

I had the same issue. The Code provided by @adrienpoly does no longer work. Here is my Solution:

new Choices(input, {
  callbackOnInit: function() {
    if(this.passedElement.element.autofocus) {
      this.showDropdown();
      this.input.focus();
    }
  }
});

Tested on Version 10.1.0

janlucaklees avatar Apr 20 '22 07:04 janlucaklees