Choices icon indicating copy to clipboard operation
Choices copied to clipboard

How to add an Item programmatically ?

Open bartoszmichalski opened this issue 3 years ago • 1 comments

How to add an Item (see terminology) programmatically ? I need to use it, when user clicks something outside choices container.

bartoszmichalski avatar Mar 30 '21 08:03 bartoszmichalski

Try using setChoices method:

first initialize choices select on a variable.

let select = new Choices({...optionsHere});

then upon loading the dom call the method:

select.setChoices(arrayOfValues, 'value', 'label', false)

note: arrayOfValues is used if using multiple select otherwise just pass in the id value of the option you wanted to be selected.

gotchas: this method currently have a bug where the pre selected items cannot be deselected using the close button and there is an open pull request to fix this issue. I'm also looking for a workaround this at the moment.

lnfel avatar May 06 '21 09:05 lnfel