百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

custom-select

> 编程语言
开源

一个轻量级的 JavaScript 库,用于自定义 HTML <select> 的创建和管理。不需要依赖其他库。

100 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

一个轻量级的 JavaScript 库,用于自定义 HTML <select> 的创建和管理。不需要依赖其他库。

custom-select

A lightweight JavaScript library for custom HTML `` creation and managing. No dependencies needed.

Demos

Base
Bootstrap
Control buttons
GSAP
jQuery
Mobile Touch Devices

Install

Install with npm (recommended)

$ npm install --save custom-select

Or download the minified build file here. (jquery version is alternative, not needed!)

Use

With ES6 modules via the import statement:

javascript
import customSelect from 'custom-select';

In CommonJs environments with the require function:

javascript
var customSelect = require("custom-select").default;

Note: the require().default is necessary due to the babelify export system.

In HTML with the script tag:

xml

and optionally:

xml

How it works

Start with a simple HTML ``:

xml

  Select...
  Foo
  Buz
javascript
customSelect('select');

You can nest the select in their label or you can use the for attribute on the label. Nested will work fine but it's formally wrong due to label element specification: only a select element can be nested in a label tag.

Here's the HTML result:

xml

    

    

  

Also state classes will be added and removed while plugin working.

You can use default css included in plugin release or style it by yourself. You can also use advanced techniques for using native select on Mobile/touch devices. Check the examples for inspirations.

Plugin init

javascript
Array customSelect(elements[, pluginOptions]);

The elements parameter could be:

A DOMString selectors:

javascript
customSelect('.myForm .mySelect');

A instance of HTMLElement, tag SELECT:

javascript
customSelect(document.getElementById('mySelect'));

A list (NodeList, HTMLCollection, Array, etc) of instances of HTMLElement, tag SELECT:

javascript
customSelect(document.querySelector('.mySelect'));
//or
customSelect(document.getElementsByClassName('mySelect'));

The pluginOptions parameter is an object that overwrites some default plugin configurations.

The default config is:

javascript
{
  containerClass: 'custom-select-container',
  openerClass: 'custom-select-opener',
  panelClass: 'custom-select-panel',
  optionClass: 'custom-select-option',
  optgroupClass: 'custom-select-optgroup',
  isSelectedClass: 'is-selected',
  hasFocusClass: 'has-focus',
  isDisabledClass: 'is-disabled',
  isOpenClass: 'is-open'
}

The return is an Array of customSelect instances, that contains all the public exposed methods and properties.

Style Classes

All css classes can be configured using pluginOptions, except container secondary class customSelect which is only for internal use and should not be removed or used for styling purpose.

Structure Classes

Self explained structure classes, and relative may-have status classes:

containerClass: 'custom-select-container' may have isDisabledClass, isOpenClass

openerClass: 'custom-select-opener'

panelClass: 'custom-select-panel'

optionClass: 'custom-select-option' may have isSelectedClass, hasFocusClass

optgroupClass: 'custom-select-optgroup'

State Classes

isSelectedClass: 'is-selected' - when the custom option is selected (as native selected attribute).

hasFocusClass: 'has-focus' - when the custom option has current focus (mouseover, arrow navigation and keyboard autocomplete changes the focus).

isDisabledClass: 'is-disabled' - when the select is disabled.

isOpenClass: 'is-open' - when the panel is open.

How to get Plugin instance

Init return

javascript
const cstSel = customSelect('select')[0]; // return is an array of instances!
console.log(cstSel.open); // true|false

The DOM select

javascript
customSelect('select');
const cstSel =  document.querySelector('select').customSelect
console.log(cstSel.open); // true|false

The DOM select container

javascript
customSelect('select');
const cstSel =  document.querySelector('.customSelect').customSelect
console.log(cstSel.open); // true|false

Methods & Properties

pluginOptions property [readonly]

Get the plugin options.

javascript
cstSel.pluginOptions;

open property

Get/set property.

javascript
cstSel.open = true; // open the custom select
console.log(cstSel.open); // true
cstSel.open = false; // close the custom select
console.log(cstSel.open); // false

disabled property

Get/set property.

javascript
cstSel.disabled = true; // disable the custom select
console.log(cstSel.disabled); // true
cstSel.disabled = false; // enable the custom select
console.log(cstSel.disabled); // false

value property

Get/set property.
Change both the native select and the custom select. Use it just like nativeSelect.value

javascript
cstSel.value = 'foo'; // the first option with that value will be selected. If there is no option with that value the first one'll be selected.
console.log(cstSel.value); // return foo if there was an option with 'foo' value

append(elements[, target]) method

Append an option or an optgroup to the select.

javascript
const option = document.createElement('option');
option.text = 'Foo';
option.value = 'bar';
cstSel.append(option);

The elements parameter could be:

An instance of HTMLElement, tag OPTION:

javascript
const toBeAppend = document.createElement('option');

An instance of HTMLElement, tag OPTGROUP:

javascript
const toBeAppend = document.createElement('optgroup');

A list (NodeList, HTMLCollection, Array, etc) of instance of HTMLElement, tag OPTION/OPTGROUP:

javascript
const toBeAppend = cstSel.empty();

The target parameter must be the select (default) or an optgroup that is already inside the select.

insertBefore(elements, target) method

insert an option or an optgroup before the specified target.

javascript
const option = document.createElement('option');
option.text = 'Foo';
option.value = 'foo';
const target = cstSel.select.options[2];
cstSel.insertBefore(option, target);

The elements parameter could be:

An instance of HTMLElement, tag OPTION:

javascript
const toBeAppend = document.createElement('option');

An instance of HTMLElement, tag OPTGROUP:

javascript
const toBeAppend = document.createElement('optgroup');

A list (NodeList, HTMLCollection, Array, etc) of instance of HTMLElement, tag OPTION/OPTGROUP:

javascript
const toBeAppend = cstSel.empty();

The target parameter must be an option or an optgroup that is already inside the select.

remove(node) method

remove an option or an optgroup

javascript
cstSel.remove(cstSel.select.options[1]);

empty() method

empty the select

javascript
cstSel.empty();

destroy() method

destroy the plugin, removing custom markup, classes, and listeners.

javascript
cstSel.destroy();

opener property [readonly]

DOM Element

select property [readonly]

DOM Element

panel property [readonly]

DOM Element

container property [readonly]

DOM Element

Events

custom-select:open

Only on container.

javascript
cstSel.container.addEventListener('custom-select:open',
  (e) => { console.log(`${e.target} is open `)});

custom-select:close

Only on container.

javascript
cstSel.container.addEventListener('custom-select:close',
  (e) => { console.log(`${e.target} is closed `)});

custom-select:disabled

Only on container.

javascript
cstSel.container.addEventListener('custom-select:disabled',
  (e) => { console.log(`${e.target} is disabled `)});

custom-select:enabled

Only on container.

javascript
cstSel.container.addEventListener('custom-select:enabled',
  (e) => { console.log(`${e.target} is enabled `)});

custom-select:focus-outside-panel

Fired on custom option, recommended listener on panel.

This CustomEvent fires when the focused option moves outside the visible part of the panel.
It bubbles, so the listener can be placed on every ancestor of the custom options.
This event is useful for custom animations on select's autocomplete-search, when the focus moves to the found option. By default there's no animation but a simply scrollTop change of the panel.
You can overwrite this behaviour by simply adding an EventListener, with useCapture argument set to true and an e.stopPropagation() statement inside you listener's callback-function.

javascript
// Example with jQuery animate
cstSel.panel.addEventListener('custom-select:focus-outside-panel',
  (e) => {
    e.stopPropagation();
    $(cstSel.panel).animate({
      scrollTop: e.target.offsetTop,
    }, 300)
  }, true);

change

Only on select.

javascript
cstSel.select.addEventListener('change',
  (e) => { console.log(`${e.target} has changed it's value `)});

jQuery adaptor

If you really can't live without jQuery, an adaptor was made for you : download jQuery version here.

jQuery init

javascript
$('#mySelect').customSelect();

jQuery property set

javascript
$('#mySelect').customSelect('open', true);

jQuery property get

javascript
$('#mySelect').customSelect('open');

jQuery methods

javascript
$('#mySelect').customSelect('remove', $('#mySelect')[0].options[1]);

That's all folks!

And now have fun ✌

Browser support

Oh wait, I was almost forgetting:

  • Chrome
  • Safari 7.0
  • Firefox 10 (maybe also older, but come on...)
  • Android 4.0
  • Mobile Safari 6.0
  • Internet Explorer 10

Issues· 27 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptselectui-components

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言