Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aarontharker's avatar

Choices.js and vite

Trying to add Choices.js to my TALL app and I can't seem to get it working. I've added the following to my app.js

import Choices from 'choices.js';
window.Choices = Choices;

but when I try to call it in my blade with the following code,

select2 = new Choices($refs.select, {
                position: 'bottom',
                searchPlaceholderValue: '{{__('Search...')}}',
                shouldSort: false,
                itemSelectText: '',
            })

I get

Uncaught ReferenceError: Choices is not defined
0 likes
6 replies
Sinnbeck's avatar

Be sure to have the vite script before the script where you try to use it

aarontharker's avatar

actually I'm not calling Choices in my script stack but in the x-init of a my component.

<div
    x-data="{
        model: @entangle($attributes->wire('model')),
    }"
    x-init="
        select2 = new Choices($refs.select, {
                position: 'bottom',
                searchPlaceholderValue: '{{__('Search...')}}',
                shouldSort: false,
                itemSelectText: '',
            })
        $refs.select.addEventListener('change', (event)=>{
            if (event.target.hasAttribute('multiple')){
                model = Array.from(event.target.options).filter(option => option.selected).map(option => option.value);
            }else{
                model = event.target.value;
            }
        });
    "
    wire:ignore
>

Please or to participate in this conversation.