Skip to content

Commit 3afb0b2

Browse files
authored
Refine README formatting and content
Updated README to improve clarity and formatting.
1 parent 7f5abb8 commit 3afb0b2

File tree

1 file changed

+59
-38
lines changed

1 file changed

+59
-38
lines changed

README.md

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
21
# Multi Select Dropdown JS
32

43
Create powerful user interfaces with our **Multi Select Dropdown**! This tool enhances native select elements, allowing for multiple selections, dynamic content generation, integrated search functionality, and customizable UI without any dependencies. No jQuery or other library is required!
54

65
The complete guide and reference is available here: [https://codeshack.io/multi-select-dropdown-html-javascript/](https://codeshack.io/multi-select-dropdown-html-javascript/)
76

8-
Demo: [https://codeshack.io/multi-select-dropdown-js/](https://codeshack.io/multi-select-dropdown-js/)
7+
Demo:[https://codeshack.io/multi-select-dropdown-js/](https://codeshack.io/multi-select-dropdown-js/)
98

109
## Features
1110
- **Multiple Selections**: Users can select more than one option in the dropdown.
11+
- **OptGroups & Sublists**: Natively parses HTML `<optgroup>` tags and automatically generates master toggle switches for groups.
12+
- **Native Form Validation**: Fully supports the HTML5 `required` attribute with perfectly positioned browser tooltips.
13+
- **Dark Mode & Themes**: Built-in support for `auto` (follows OS preference), `light`, and `dark` themes.
14+
- **Async Data Fetching**: Easily load external JSON arrays from APIs using the built-in `.fetch()` method.
1215
- **Search Functionality**: Includes a built-in search to find options quickly.
13-
- **Dynamic Content**: Options can be dynamically added to the dropdown.
14-
- **Customizable UI**: Style the dropdown to match your site with minimal CSS.
16+
- **Dynamic & Reactive**: Update options dynamically via JS setters. The UI instantly reacts to data changes.
17+
- **Secure & Accessible**: Built-in XSS protection, memory-leak proof, framework-agnostic, and fully keyboard navigable.
1518
- **Lightweight**: Lightweight in size and does not depend on other libraries.
16-
- **Flexible**: Convert your existing native select elements with minimal configuration.
1719

1820
## Screenshot
1921

@@ -33,39 +35,50 @@ Append the following to the **body** tag:
3335

3436
## Usage
3537

36-
Here's a simple example to add the multi-select dropdown to your project:
38+
Here's a simple example to add the multi-select dropdown to your project.
39+
40+
*(Tip: Always append `[]` to your `name` attribute if you are submitting data to a backend like PHP!)*
3741

3842
```html
39-
<select id="example-multi-select" data-placeholder="Select options" multiple="multiple" data-multi-select>
43+
<select id="example-multi-select" name="options[]" data-placeholder="Select options" multiple data-multi-select>
4044
<option value="option1">Option 1</option>
4145
<option value="option2">Option 2</option>
4246
<!-- more options -->
4347
</select>
4448
```
45-
Or:
49+
Or initialize it manually via JavaScript:
4650
```html
47-
<select id="example-multi-select" data-placeholder="Select options" multiple="multiple">
51+
<select id="example-multi-select" name="options[]" multiple>
4852
<option value="option1">Option 1</option>
4953
<option value="option2">Option 2</option>
5054
<!-- more options -->
5155
</select>
5256

5357
<script src="path/to/multi-select-dropdown.js"></script>
5458
<script>
55-
new MultiSelect(document.getElementById('example-multi-select'));
59+
new MultiSelect(document.getElementById('example-multi-select'), {
60+
placeholder: 'Select options',
61+
theme: 'auto'
62+
});
5663
</script>
5764
```
5865

5966
## Configuration
6067

61-
You can pass a settings object to customize the dropdown:
68+
You can pass a settings object to customize the dropdown behavior and styling:
6269

6370
```javascript
64-
new MultiSelect(document.getElementById('example-multi-select'), {
71+
new MultiSelect('#example-multi-select', {
6572
placeholder: 'Select options',
73+
theme: 'auto', // 'auto', 'light', or 'dark'
74+
min: 1, // Minimum number of items required
6675
max: 5, // Maximum number of items that can be selected
6776
search: true, // Enable the search box
68-
selectAll: true, // Add a select all option
77+
selectAll: true, // Add a "Select All" toggle
78+
listAll: true, // If false, shows "X selected" instead of listing tags
79+
closeListOnItemSelect: false, // Auto-close the dropdown after selection
80+
required: true, // Enforce native HTML5 form validation
81+
6982
onChange: function(value, text, element) {
7083
console.log('Change:', value, text, element);
7184
},
@@ -74,51 +87,59 @@ new MultiSelect(document.getElementById('example-multi-select'), {
7487
},
7588
onUnselect: function(value, text, element) {
7689
console.log('Unselected:', value, text, element);
90+
},
91+
onMaxReached: function(max) {
92+
console.log('Maximum selections reached:', max);
7793
}
7894
});
7995
```
8096

81-
Also, you can dynamically add the options:
97+
### Dynamically Adding Options & Groups
98+
99+
You can dynamically populate the dropdown with grouped data, disabled options, and safe custom HTML injection:
82100

83101
```javascript
84-
new MultiSelect(document.getElementById('example-multi-select'), {
85-
data: [
102+
const mySelect = new MultiSelect('#example-multi-select', {
103+
data:[
86104
{
87105
value: 'opt1',
88-
text: 'Option 1'
106+
text: 'Option 1',
107+
group: 'Basic Settings'
89108
},
90109
{
91110
value: 'opt2',
92-
html: '<strong>Option 2 with HTML!</strong>'
111+
html: '<strong style="color: red;">Option 2 with HTML!</strong>',
112+
group: 'Basic Settings'
93113
},
94114
{
95115
value: 'opt3',
96116
text: 'Option 3',
97-
selected: true
117+
selected: true,
118+
group: 'Advanced Settings'
98119
},
99120
{
100121
value: 'opt4',
101-
text: 'Option 4'
102-
},
103-
{
104-
value: 'opt5',
105-
text: 'Option 5'
122+
text: 'Locked Option',
123+
disabled: true // Prevents user interaction
106124
}
107125
],
108126
placeholder: 'Select options',
109-
max: 5, // Maximum number of items that can be selected
110-
search: true, // Enable the search box
111-
selectAll: true, // Add a select all option
112-
onChange: function(value, text, element) {
113-
console.log('Change:', value, text, element);
114-
},
115-
onSelect: function(value, text, element) {
116-
console.log('Selected:', value, text, element);
117-
},
118-
onUnselect: function(value, text, element) {
119-
console.log('Unselected:', value, text, element);
120-
}
127+
search: true,
128+
selectAll: true
129+
});
130+
```
131+
132+
### Asynchronous Data Fetching
133+
134+
Loading data from a remote endpoint is handled natively:
135+
136+
```javascript
137+
const asyncSelect = new MultiSelect('#example-multi-select', {
138+
placeholder: 'Loading remote data...'
121139
});
140+
141+
// Fetches a JSON array formatted like the data object above
142+
asyncSelect.fetch('https://api.yoursite.com/endpoint');
122143
```
123144

124145
## License
@@ -129,8 +150,8 @@ Distributed under the MIT License. See `LICENSE` for more information.
129150

130151
David Adams - [info@codeshack.io](mailto:info@codeshack.io)
131152

132-
GitHub: [https://github.com/codeshackio/multi-select-dropdown-js](https://github.com/codeshackio/multi-select-dropdown-js)
153+
GitHub:[https://github.com/codeshackio/multi-select-dropdown-js](https://github.com/codeshackio/multi-select-dropdown-js)
133154

134-
X (Twitter): [https://twitter.com/codeshackio](https://twitter.com/codeshackio)
155+
X (Twitter):[https://twitter.com/codeshackio](https://twitter.com/codeshackio)
135156

136157
Feel free to open an issue or submit pull requests.

0 commit comments

Comments
 (0)