A powerful, lightweight, and highly customizable JavaScript library that transforms HTML select elements into modern, searchable dropdowns with mobile-responsive design.
β οΈ Development Status: This project is currently in active development. This is the initial MVP (Minimum Viable Product) release. Features may change and additional functionality will be added in future versions.
- π Search functionality - Filter options as you type
- π± Mobile responsive - Automatic modal interface for mobile devices
- π¨ Highly customizable - Easy styling and theming
- π Zero dependencies - Pure JavaScript implementation
- βΏ Accessibility friendly - Proper ARIA attributes and keyboard navigation
- π·οΈ Optgroup support - Organize options with groups
- π Form integration - Works seamlessly with forms and validation
- π― Event handling - Custom events for interaction tracking
- π§ API methods - Programmatic control over select elements
<script src="https://cdn.jsdelivr.net/gh/pedrohrigolin/[email protected]/selectJS.min.js"></script><script src="path/to/selectJS.js"></script>π‘ Note: The library includes all necessary CSS styles. No additional external files required.
- HTML Structure
<select class="selectJS" name="country">
<option value="us">United States</option>
<option value="ca">Canada</option>
<option value="uk">United Kingdom</option>
</select>- Initialize
// Initialize all select elements with class 'selectJS'
selectJS.all();
// Or initialize a specific element
const mySelect = document.querySelector('#my-select');
selectJS.element(mySelect);Transforms all select elements with class selectJS into SelectJS components.
Transforms a specific select element into a SelectJS component.
Parameters:
selectElement(HTMLSelectElement) - The select element to transform
Replaces the default styles with custom CSS.
selectJS.setStyle(`
.selectJS {
width: 300px;
font-family: Arial, sans-serif;
}
.selectJS-searchInput {
padding: 15px;
border-radius: 10px;
}
`);Adds additional CSS rules to the existing styles.
selectJS.addStyle(`
.selectJS-option:hover {
background-color: #e3f2fd;
}
`);Disables a SelectJS component.
Enables a previously disabled SelectJS component.
Sets or removes the required attribute.
Parameters:
selectElement(HTMLDivElement) - The SelectJS componentstate(boolean) - True to make required, false to remove
Sets or removes the readonly attribute.
Programmatically sets the value of a SelectJS component.
const mySelect = document.querySelector('.selectJS');
selectJS.setValue(mySelect, 'us');Gets the current value of a SelectJS component.
const value = selectJS.getValue(mySelect);
console.log(value); // 'us'Retrieves values from all SelectJS components on the page.
// Get all values using 'name' attribute as key
const values = selectJS.getAllValues();
// Get all values using 'id' attribute as key
const valuesById = selectJS.getAllValues({ keyType: 'id' });
// Get all values with complete information
const allData = selectJS.getAllValues({ includeAll: true });Parameters:
options.keyType(string) - 'name' or 'id' (default: 'name')options.includeAll(boolean) - Return complete object with id, name, and value (default: false)
SelectJS dispatches custom events for better integration:
Fired when a SelectJS component is created.
document.addEventListener('create', function(e) {
console.log('SelectJS created:', e.target);
});Fired when the value changes, providing both old and new values.
document.addEventListener('selectJS.change', function(e) {
console.log('Value changed from', e.detail.oldValue, 'to', e.detail.newValue);
console.log('Target:', e.detail.target);
console.log('Input:', e.detail.input);
});SelectJS supports various HTML attributes for enhanced functionality:
<select class="selectJS"
name="country"
placeholder="Choose a country..."
selectJS-id="country-select"
selectJS-modalID="country-modal"
selectJS-modalBoxID="country-modal-box"
required>
<optgroup label="North America">
<option value="us">United States</option>
<option value="ca">Canada</option>
</optgroup>
<optgroup label="Europe">
<option value="uk" selected>United Kingdom</option>
<option value="fr">France</option>
<option value="de" default>Germany</option>
</optgroup>
</select>Supported Attributes:
name- Form field nameplaceholder- Placeholder text for search inputselectJS-id- Custom ID for the hidden inputselectJS-modalID- Custom ID for mobile modalselectJS-modalBoxID- Custom ID for modal content boxrequired- Makes the field requireddisabled- Disables the selectreadonly- Makes the select readonlyselected- Marks an option as selecteddefault- Sets a default fallback option
SelectJS integrates seamlessly with HTML forms:
<form id="user-form">
<select class="selectJS" name="country" required>
<option value="us">United States</option>
<option value="ca">Canada</option>
</select>
<button type="submit">Submit</button>
</form>selectJS.all();
document.getElementById('user-form').addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
console.log('Country:', formData.get('country'));
});SelectJS automatically detects mobile devices and switches to a modal interface for better user experience on small screens.
SelectJS uses the following CSS classes that you can customize:
.selectJS- Main container.selectJS-searchInput- Search/display input.selectJS-container- Dropdown container.selectJS-option- Individual options.selectJS-optgroup- Option groups.selectJS-optgroupTitle- Option group titles.selectJS-modal- Mobile modal backdrop.selectJS-modalBox- Mobile modal content.selectJS-modalSearchInput- Mobile search input.selectJS-modalOption- Mobile modal options
Create beautiful themes by customizing the CSS:
selectJS.setStyle(`
/* Dark theme example */
.selectJS-searchInput {
background-color: #2d2d2d;
color: #ffffff;
border: 1px solid #555;
}
.selectJS-option {
background-color: #2d2d2d;
color: #ffffff;
}
.selectJS-option:hover {
background-color: #404040;
}
`);SelectJS works in all modern browsers:
- β Chrome 60+
- β Firefox 55+
- β Safari 11+
- β Edge 79+
- β Mobile browsers
- Automatic detection of mobile devices
- Modal interface for better touch interaction
- Large touch targets for easy selection
- Full-screen search on mobile devices
- Lightweight - Only ~15KB minified
- No dependencies - Pure JavaScript
- Efficient rendering - Virtual scrolling for large datasets
- Memory optimized - Proper cleanup and event management
As this is an MVP release, here are some planned features for future versions:
- π Enhanced keyboard navigation (Arrow keys, Enter, Escape)
- π― Multiple selection support
- π Internationalization (i18n) support
- π¨ Built-in themes (Dark, Light, Material, etc.)
- β‘ Virtual scrolling for large datasets
- π Plugin system for extensions
- π Better accessibility features
- π§ͺ Unit tests and documentation improvements
Contributions are welcome! Since this project is in active development, please feel free to:
- π Report bugs and issues
- π‘ Suggest new features
- π Improve documentation
- π§ Submit Pull Requests
Getting Started:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Note: As this is an MVP, breaking changes may occur between versions until we reach a stable 1.0.0 release.
This project is licensed under the MIT License - see the LICENSE file for details.
Pedro Rigolin
- GitHub: @pedrohrigolin
- Inspired by the need for better select elements in modern web applications
- Built with mobile-first approach in mind
- Special thanks to the web development community for feedback and suggestions
β Star this repository if you find it helpful!