Choices/public/test/select-multiple/index.html
2025-10-30 07:51:23 +00:00

1237 lines
42 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width,initial-scale=1,viewport-fit=cover"
/>
<title>Choices</title>
<meta
name="description"
itemprop="description"
content="A lightweight, configurable select box/text input plugin. Similar to Select2 and Selectize but without the jQuery dependency."
/>
<link
rel="apple-touch-icon"
sizes="180x180"
href="../../assets/images/apple-touch-icon.png"
/>
<link
rel="icon"
type="image/png"
href="../../assets/images/favicon-32x32.png"
sizes="32x32"
/>
<link
rel="icon"
type="image/png"
href="../../assets/images/favicon-16x16.png"
sizes="16x16"
/>
<link rel="manifest" href="../../assets/images/manifest.json" />
<link
rel="mask-icon"
href="../../assets/images/safari-pinned-tab.svg"
color="#005F75"
/>
<link rel="shortcut icon" href="../../assets/images/favicon.ico" />
<meta
name="msapplication-config"
content="../../assets/images/browserconfig.xml"
/>
<meta name="theme-color" content="#ffffff" />
<!-- Ignore these -->
<link rel="stylesheet" href="../../assets/styles/base.min.css" />
<!-- End ignore these -->
<!-- Choices includes -->
<link rel="stylesheet" href="../../assets/styles/choices.min.css" />
<script src="../../assets/scripts/choices.js"></script>
<!-- End Choices includes -->
</head>
<body>
<div class="container">
<div class="section">
<h2>Select multiple inputs</h2>
<div data-test-hook="basic">
<label for="choices-basic">Basic</label>
<button class="disable push-bottom">Disable</button>
<button class="enable push-bottom">Enable</button>
<select
class="form-control"
name="choices-basic"
id="choices-basic"
multiple
>
<option value="Choice 1"><!-- html comment -->Choice 1</option>
<option value="Choice 2" label="Choice 2" />
<option value="Find me">Choice 3</option>
<option value="Choice 4">Choice 4</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
const choicesBasic = new Choices('#choices-basic', {
allowHTML: false,
});
document
.querySelector('button.disable')
.addEventListener('click', () => {
choicesBasic.disable();
});
document
.querySelector('button.enable')
.addEventListener('click', () => {
choicesBasic.enable();
});
});
</script>
</div>
<div data-test-hook="single-item">
<label for="choices-basic">Single-item</label>
<select class="form-control" name="choices-single-item" id="choices-single-item">
<option value="Choice 1">Choice 1</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-single-item', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="render-selected-choices">
<label for="choices-remove-button">Render selected choices</label>
<select
class="form-control"
name="render-selected-choices"
id="render-selected-choices"
multiple
>
<option value="Choice 1" selected>Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4">Choice 4</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#render-selected-choices', {
allowHTML: true,
renderSelectedChoices: true,
});
});
</script>
</div>
<div data-test-hook="selected-choice-in-dropdown">
<label for="selected-choice-in-dropdown">Selected choice in dropdown</label>
<select
class="form-control"
name="selected-choice-in-dropdown"
id="selected-choice-in-dropdown"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3" selected>Choice 3</option>
<option value="Choice 4" selected>Choice 4</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#selected-choice-in-dropdown', {
allowHTML: true,
renderSelectedChoices: true,
});
});
</script>
</div>
<div data-test-hook="remove-button">
<label for="choices-remove-button">Remove button</label>
<select
class="form-control"
name="choices-remove-button"
id="choices-remove-button"
multiple
>
<option value="Choice 1" selected>Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4">Choice 4</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-remove-button', {
allowHTML: true,
removeItemButton: true,
});
});
</script>
</div>
<div data-test-hook="no-press-to-select">
<label for="no-press-to-select">No press to select</label>
<select
class="form-control"
name="no-press-to-select"
id="no-press-to-select"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4">Choice 4</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#no-press-to-select', {
allowHTML: true,
itemSelectText: '',
});
});
</script>
</div>
<div data-test-hook="duplicate-items-allowed">
<label for="duplicate-items-allowed">Duplicate items allowed</label>
<select class="form-control" name="no-choices" id="duplicate-items-allowed" multiple>
<option value="call1">Service 1</option>
<option value="call2">Service 2</option>
<option value="call2">Service 3</option>
<option value="call1">Service 4</option>
<option value="call5">Service 5</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#duplicate-items-allowed', {
duplicateItemsAllowed: true,
});
});
</script>
</div>
<div data-test-hook="duplicate-items-disallowed">
<label for="duplicate-items-disallowed">Duplicate items disallowed</label>
<select class="form-control" name="no-choices" id="duplicate-items-disallowed" multiple>
<option value="call1">Service 1</option>
<option value="call2">Service 2</option>
<option value="call2">Service 3</option>
<option value="call1">Service 4</option>
<option value="call5">Service 5</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#duplicate-items-disallowed', {
duplicateItemsAllowed: false,
});
});
</script>
</div>
<div data-test-hook="renderSelectedChoices-true">
<label for="renderSelectedChoices-true">Render selected choices (single option)</label>
<select class="form-control" name="no-choices" id="renderSelectedChoices-true" multiple>
<option value="Choice 1" selected>Choice 1</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#renderSelectedChoices-true', {
renderSelectedChoices: true,
});
});
</script>
</div>
<div data-test-hook="no-choices">
<label for="no-choices">No choices</label>
<select class="form-control" name="no-choices" id="no-choices" multiple>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#no-choices', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="no-choices2">
<label for="no-choices2">No choices (besides selected)</label>
<select class="form-control" name="no-choices" id="no-choices2" multiple>
<option value="Choice 1" selected="selected">Choice 1</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#no-choices2', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="disabled-choice">
<label for="choices-disabled-choice">Disabled choice</label>
<select
class="form-control"
name="choices-disabled-choice"
id="choices-disabled-choice"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4" disabled>Choice 4</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-disabled-choice', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="disabled-first-choice-via-options">
<label for="choices-disabled-choice-via-options"
>Disabled first choice by options</label
>
<select
class="form-control"
name="choices-disabled-choice-via-options"
id="choices-disabled-choice-via-options"
multiple
>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-disabled-choice-via-options', {
allowHTML: true,
removeItemButton: true,
choices: [
{
value: 'Choice 1',
label: 'Choice 1',
disabled: true,
},
{
value: 'Choice 2',
label: 'Choice 2',
},
{
value: 'Choice 3',
label: 'Choice 3',
},
{
value: 'Choice 4',
label: 'Choice 4',
},
],
});
});
</script>
</div>
<div data-test-hook="disabled-via-fieldset">
<fieldset disabled>
<label for="choices-disabled-via-fieldset">Disabled via fieldset</label>
<select
class="form-control"
name="choices-disabled-via-fieldset"
id="choices-disabled-via-fieldset"
multiple
>
<option value="Choice 1" selected>Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
</fieldset>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-disabled-via-fieldset', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="disabled-via-attr">
<label for="choices-disabled-via-attr">Disabled via attribute</label>
<select
class="form-control"
name="choices-disabled-via-attr"
id="choices-disabled-via-attr"
multiple
disabled
>
<option value="Choice 1" selected>Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-disabled-via-attr', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="selection-limit">
<label for="choices-selection-limit">Input limit</label>
<select
class="form-control"
name="choices-selection-limit"
id="choices-selection-limit"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4">Choice 4</option>
<option value="Choice 5">Choice 5</option>
<option value="Choice 6">Choice 6</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-selection-limit', {
allowHTML: true,
maxItemCount: 5,
});
});
</script>
</div>
<div data-test-hook="selection-limit-note-after-unselecting-choice">
<label for="choices-selection-limit-note-after-unselecting-choice">Input limit note after unselecting choice</label>
<select
class="form-control"
name="choices-selection-limit-note-after-unselecting-choice"
id="choices-selection-limit-note-after-unselecting-choice"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4">Choice 4</option>
<option value="Choice 5">Choice 5</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-selection-limit-note-after-unselecting-choice', {
allowHTML: true,
maxItemCount: 5,
removeItemButton: true
});
});
</script>
</div>
<div data-test-hook="prepend-append">
<label for="choices-prepend-append">Prepend/append</label>
<select
class="form-control"
name="choices-prepend-append"
id="choices-prepend-append"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-prepend-append', {
allowHTML: true,
prependValue: 'before-',
appendValue: '-after',
});
});
</script>
</div>
<div data-test-hook="render-choice-limit">
<label for="choices-render-choice-limit">Render choice limit</label>
<select
class="form-control"
name="choices-render-choice-limit"
id="choices-render-choice-limit"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-render-choice-limit', {
allowHTML: true,
renderChoiceLimit: 1,
});
});
</script>
</div>
<div data-test-hook="search-floor">
<label for="choices-search-floor">Search floor</label>
<select
class="form-control"
name="choices-search-floor"
id="choices-search-floor"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-search-floor', {
allowHTML: true,
searchFloor: 5,
});
});
</script>
</div>
<div data-test-hook="search-hide-selected">
<label for="choices-search-hide-selected">Search hide selected</label>
<select
class="form-control"
name="choices-search-hide-selected"
id="choices-search-hide-selected"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4">Choice 4</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-search-hide-selected', {
allowHTML: false,
searchRenderSelectedChoices: false,
});
});
</script>
</div>
<div data-test-hook="placeholder-via-option-value">
<label for="choices-placeholder-via-option-value"
>Placeholder via empty option value</label
>
<select
class="form-control"
name="choices-placeholder-via-option-value"
id="choices-placeholder-via-option-value"
multiple
>
<option value="">I am a placeholder</option>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-placeholder-via-option-value', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="placeholder-via-option-attr">
<label for="choices-placeholder-via-option-attr"
>Placeholder via option attribute</label
>
<select
class="form-control"
name="choices-placeholder-via-option-attr"
id="choices-placeholder-via-option-attr"
multiple
>
<option placeholder>I am a placeholder</option>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-placeholder-via-option-attr', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="placeholder-via-data-attr">
<label for="choices-placeholder-via-data-attr"
>Placeholder via data attribute</label
>
<select
class="form-control"
name="choices-placeholder-via-data-attr"
id="choices-placeholder-via-data-attr"
data-placeholder="I am a placeholder"
multiple
>
<option value="">I am a not placeholder</option>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-placeholder-via-data-attr', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="remote-data">
<label for="choices-remote-data">Remote data</label>
<select
class="form-control"
name="choices-remote-data"
id="choices-remote-data"
multiple
>
<option value="">I am a placeholder</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-remote-data', {
allowHTML: true,
shouldSort: false,
}).setChoices(async () => {
try {
const data = await fetch('../data.json');
return data.json();
} catch (e) {
console.log(e);
return [];
}
});
});
</script>
</div>
<div data-test-hook="scrolling-dropdown">
<label for="choices-scrolling-dropdown">Scrolling dropdown</label>
<select
class="form-control"
name="choices-scrolling-dropdown"
id="choices-scrolling-dropdown"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4">Choice 4</option>
<option value="Choice 5">Choice 5</option>
<option value="Choice 6">Choice 6</option>
<option value="Choice 7">Choice 7</option>
<option value="Choice 8">Choice 8</option>
<option value="Choice 9">Choice 9</option>
<option value="Choice 10">Choice 10</option>
<option value="Choice 11">Choice 11</option>
<option value="Choice 12">Choice 12</option>
<option value="Choice 13">Choice 13</option>
<option value="Choice 14">Choice 14</option>
<option value="Choice 15">Choice 15</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-scrolling-dropdown', {
allowHTML: true,
shouldSort: false,
});
});
</script>
</div>
<div data-test-hook="groups">
<label for="choices-groups">Choice groups</label>
<select
class="form-control"
name="choices-groups"
id="choices-groups"
multiple
>
<optgroup label="UK">
<option value="London">London</option>
<option value="Manchester">Manchester</option>
<option value="Liverpool">Liverpool</option>
</optgroup>
<optgroup label="FR">
<option value="Paris">Paris</option>
<option value="Lyon">Lyon</option>
<option value="Marseille">Marseille</option>
</optgroup>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-groups', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="mixed-groups">
<label for="mixed-choices-groups"
>Choice groups with some elements without a group</label
>
<select
class="form-control"
name="mixed-choices-groups"
id="mixed-choices-groups"
multiple
>
<optgroup label="UK">
<option value="London">London</option>
</optgroup>
<option value="Paris">Paris</option>
<option value="Lyon">Lyon</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#mixed-choices-groups', {
allowHTML: false,
});
});
</script>
</div>
<div data-test-hook="custom-properties">
<label for="choices-custom-properties">Custom properties</label>
<select
class="form-control"
name="choices-custom-properties"
id="choices-custom-properties"
multiple
></select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-custom-properties', {
allowHTML: true,
searchFields: ['label', 'value', 'customProperties.country'],
choices: [
{
id: 1,
value: 'London',
label: 'London',
customProperties: {
country: 'United Kingdom',
},
},
{
id: 2,
value: 'Berlin',
label: 'Berlin',
customProperties: {
country: 'Germany',
},
},
{
id: 3,
value: 'Lisbon',
label: 'Lisbon',
customProperties: {
country: 'Portugal',
},
},
],
});
});
</script>
</div>
<div data-test-hook="custom-properties-html">
<label for="choices-custom-properties-html">Custom properties</label>
<select
class="form-control"
name="choices-custom-properties-html"
id="choices-custom-properties-html"
>
<option value="Dropdown item 1">Label One</option>
<option value="Dropdown item 2">Label Two</option>
<option
value="Dropdown item 3"
data-custom-properties="This option is fantastic"
>Label Three</option
>
<option
value="Dropdown item 4"
data-custom-properties="{ 'description': 'foo' }"
>Label Four</option
>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-custom-properties-html', {
allowHTML: true,
searchFields: ['label', 'value', 'customProperties'],
});
});
</script>
</div>
<div data-test-hook="non-string-values">
<label for="choices-non-string-values">Non-string values</label>
<select
class="form-control"
name="choices-non-string-values"
id="choices-non-string-values"
></select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-non-string-values', {
allowHTML: true,
choices: [
{
id: 1,
label: 'Number',
value: 1,
},
{
id: 2,
label: 'Boolean',
value: true,
},
{
id: 3,
label: 'Object',
value: {
test: true,
},
},
{
id: 4,
label: 'Array',
value: ['test'],
},
],
});
});
</script>
</div>
<div data-test-hook="within-form">
<form>
<label for="choices-within-form">Within form</label>
<select
class="form-control"
name="choices-within-form"
id="choices-within-form"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
</select>
</form>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-within-form', {
allowHTML: true,
});
});
</script>
</div>
<div data-test-hook="set-choice-by-value">
<label for="choices-set-choice-by-value"
>Dynamically set choice by value</label
>
<select
class="form-control"
name="choices-set-choice-by-value"
id="choices-set-choice-by-value"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-set-choice-by-value', {
allowHTML: true,
}).setChoiceByValue('Choice 2');
});
</script>
</div>
<div data-test-hook="search-by-label">
<label for="choices-search-by-label">Search by label</label>
<select
class="form-control"
name="choices-search-by-label"
id="choices-search-by-label"
multiple
>
<option value="value1">label1</option>
<option value="value2">label2</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-search-by-label', {
allowHTML: true,
searchFields: ['label']
});
});
</script>
</div>
<div data-test-hook="allowhtml-undefined">
<label for="choices-allowhtml-undefined">HTML disabled by default (adds choices)</label>
<select
class="form-control"
name="choices-allowhtml-undefined"
id="choices-allowhtml-undefined"
multiple
></select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-allowhtml-undefined', {
addChoices: true,
choices: [
{
id: 1,
label: '<b>Choice 1</b>',
value: 'Choice 1',
selected: true
},
{
id: 2,
label: '<b>Choice 2</b>',
value: 'Choice 2',
},
{
id: 3,
label: 'Choice 3',
value: 'Choice 3',
},
],
});
});
</script>
</div>
<div data-test-hook="allowhtml-true">
<label for="choices-allowhtml-true">HTML allowed (adds choices)</label>
<select
class="form-control"
name="choices-allowhtml-true"
id="choices-allowhtml-true"
multiple
></select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-allowhtml-true', {
allowHTML: true,
allowHtmlUserInput: true,
addChoices: true,
choices: [
{
id: 1,
label: '<b>Choice 1</b>',
value: 'Choice 1',
selected: true
},
{
id: 2,
label: '<b>Choice 2</b>',
value: 'Choice 2',
},
{
id: 3,
label: 'Choice 3',
value: 'Choice 3',
},
],
});
});
</script>
</div>
<div data-test-hook="allowhtml-true-userinput-false">
<label for="choices-allowhtml-true-userinput-false">HTML allowed (except user input, adds choices)</label>
<select
class="form-control"
name="choices-allowhtml-true-userinput-false"
id="choices-allowhtml-true-userinput-false"
multiple
></select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-allowhtml-true-userinput-false', {
allowHTML: true,
allowHtmlUserInput: false,
addChoices: true,
choices: [
{
id: 1,
label: '<b>Choice 1</b>',
value: 'Choice 1',
selected: true
},
{
id: 2,
label: '<b>Choice 2</b>',
value: 'Choice 2',
},
{
id: 3,
label: 'Choice 3',
value: 'Choice 3',
},
],
});
});
</script>
</div>
<div data-test-hook="allowhtml-false">
<label for="choices-allowhtml-false">HTML disabled (adds choices)</label>
<select
class="form-control"
name="choices-allowhtml-false"
id="choices-allowhtml-false"
multiple
></select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#choices-allowhtml-false', {
allowHTML: false,
allowHtmlUserInput: false,
addChoices: true,
choices: [
{
id: 1,
label: '<b>Choice 1</b>',
value: 'Choice 1',
selected: true
},
{
id: 2,
label: '<b>Choice 2</b>',
value: 'Choice 2',
},
{
id: 3,
label: 'Choice 3',
value: 'Choice 3',
},
],
});
});
</script>
</div>
<div data-test-hook="new-destroy-init">
<label for="choices-new-destroy-init">New, Destroy, Init</label>
<select
class="form-control"
name="choices-new-destroy-init"
id="choices-new-destroy-init"
multiple
>
<option value="Choice 1">Choice 1</option>
<optgroup label="test">
<option value="Choice 2">Choice 2</option>
</optgroup>
<option value="Choice 3">Choice 3</option>
</select>
<button class="destroy">Destroy</button>
<button class="init">Init</button>
<script>
document.addEventListener('DOMContentLoaded', function() {
const newDestroyInitChoices = new Choices('#choices-new-destroy-init', {
allowHTML: true,
});
document
.querySelector('button.destroy')
.addEventListener('click', () => {
newDestroyInitChoices.destroy();
});
document.querySelector('button.init').addEventListener('click', () => {
newDestroyInitChoices.init();
});
});
</script>
</div>
<div data-test-hook="shadow-dom">
<noscript style="display:none;">
<link rel="stylesheet" href="../../assets/styles/base.min.css" />
<link rel="stylesheet" href="../../assets/styles/choices.min.css" />
<script src="../../assets/scripts/choices.min.js"></script>
<label for="shadow-dom-choices-basic">Shadow DOM - Basic</label>
<button class="disable push-bottom">Disable</button>
<button class="enable push-bottom">Enable</button>
<select
class="form-control"
name="shadow-dom-choices-basic"
id="shadow-dom-choices-basic"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Find me">Choice 3</option>
<option value="Choice 4">Choice 4</option>
</select>
</noscript>
<div data-shadow-dom></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const shadowDomTest = document.querySelector('div[data-test-hook="shadow-dom"]'),
payload = shadowDomTest.querySelector('noscript'),
shadowDomHtml = payload.innerHTML,
shadowDomWrapper = shadowDomTest.querySelector('div[data-shadow-dom]');
payload.innerHTML = ''; // ensure the shadow-dom example is not part of the main document
const shadowRoot = shadowDomWrapper.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = shadowDomHtml;
new Choices('#shadow-dom-choices-basic', {
allowHTML: false,
shadowRoot: shadowRoot,
});
});
</script>
</div>
<div data-test-hook="autocomplete">
<label for="choices-autocomplete">Autocomplete example</label>
<select
class="form-control"
name="choices-autocomplete"
id="choices-autocomplete"
multiple
>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
const choices = new Choices('#choices-autocomplete', {
shouldSort: false,
});
choices.passedElement.element.addEventListener(
'search',
function(e) {
const query = e.detail.value
if (query.length < 2) {
choices.clearChoices()
return
}
let result = []
if (query.slice(0, 2) === 'fo') {
result = [{ value: 'found', label: 'Found' }]
}
choices.setChoices(
result,
'value',
'label',
true
)
}
)
});
</script>
</div>
<div data-test-hook="clear-on-add">
<label for="choices-clear-on-add">Clear choices on add item</label>
<select
class="form-control"
name="choices-clear-on-add"
id="choices-clear-on-add"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
const choices = new Choices('#choices-clear-on-add', {});
choices.passedElement.element.addEventListener("addItem", function () {
choices.clearChoices()
choices.hideDropdown()
})
});
</script>
</div>
<div data-test-hook="set-choices-preserve">
<label for="choices-set-choices-preserve">setChoices example</label>
<select
class="form-control"
name="choices-set-choices-preserve"
id="choices-set-choices-preserve"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2" selected>Choice 2</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
const choices = new Choices('#choices-set-choices-preserve', {});
choices.setChoices(
[
{ value: 'Choice 1', label: 'Choice 1' },
{ value: 'Choice 2', label: 'Choice 2' }
],
'value',
'label',
true,
);
});
</script>
</div>
<div data-test-hook="set-choices-preserve-no-dupes">
<label for="choices-set-choices-preserve">setChoices example (no duplicates)</label>
<select
class="form-control"
name="choices-set-choices-preserve-no-dupes"
id="choices-set-choices-preserve-no-dupes"
multiple
>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2" selected>Choice 2</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
const choices = new Choices('#choices-set-choices-preserve-no-dupes', {
duplicateItemsAllowed: false,
});
choices.setChoices(
[
{ value: 'Choice 1', label: 'Choice 1' },
{ value: 'Choice 2', label: 'Choice 2' }
],
'value',
'label',
true,
);
});
</script>
</div>
</div>
</div>
</body>
</html>