Choices-js / Choices

A vanilla JS customisable select box/text input plugin ⚡️
https://choices-js.github.io/Choices/
MIT License
6.06k stars 597 forks source link

Instantiate choices when select field added dynamically #1065

Open creativetags opened 1 year ago

creativetags commented 1 year ago

I'm using Choices for select fields on a form and when one field is changed I do an AJAX request to the server and the server sends back another select field to add to the form. I need that field to be instantiated as a Choices select.

I'm instantiating my original fields with:

const items = document.querySelectorAll('.js-choice'); items.forEach((item) => new Choices(item));

but when I try to add something like that to my AJAX code it complains that Choices isn't defined and it won't let me import it again because I'm not calling from a js module.

Is there a way to watch for new .js-choice elements and instantiate automatically?

Also posted to StackOverflow