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?
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