Choices-js / Choices

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

Given an element how can I get choice instance? #1075

Closed realnot closed 1 month ago

realnot commented 1 year ago

I'm generating all choices instances on elements that have a 'data-choices' on application bootstrap. Then, I have to update choices set from API.

const element = document.getElementById('id_city');
const choices = new window.Choices(element)
{#choices.setChoices(data['results'], 'pk', 'name', true);#}
choices.setChoices([
  { value: 'One', label: 'Label One', disabled: true },
  { value: 'Two', label: 'Label Two', selected: true },
  { value: 'Three', label: 'Label Three' },
]);

setChoices in this case will not work because an instance of choices is already initialized on 'element'. How can retrieve choices instance from 'element'?

b3wii commented 8 months ago

+1

Hel5ing commented 3 months ago

+11111

Xon commented 1 month ago

This requires infrastructure outside choices to track the HTML elements and javascript object relationships, and is well outside the scope of what this project can offer