Make select-box or combo-box by one module!
Download cccombo.js
and cccombo.css
files,
and include them into your application.
Then initialize Cccombo like this:
document.addEventListener('DOMContentLoaded', function() {
var cccomboArray = document.getElementsByClassName('cccombo');
[].forEach.call(cccomboArray, function(element) {
element.cccombo = new Cccombo(element);
element.cccombo.init();
});
});
<div class="cccombo">
<button type="button"></button>
<ul>
<li>The first option</li>
<li>The second option</li>
<li>The third option</li>
</ul>
</div>
<div class="cccombo">
<input type="text" />
<ul>
<li>The first option</li>
<li>The second option</li>
<li>The third option</li>
</ul>
</div>
Just add data-label
attribute to the target element inside <button>
.
<button type="button">
<span data-label></span>
</button>
Just add data-label
attribute to the li
element:
<li data-label="The first">
The first option
</li>
Or to its child element with the necessary label-content (helpful for custom HTML-labels):
<li>
<span data-label><img src="icon.png" /></span> The second option
</li>
Just add data-value
attribute to <li>
elements.
<li data-value="1">The first option</li>
<li data-value="2">The second option</li>
<li data-value="3">The third option</li>
Add group
class to group-header elements.
Group-header items become unselectable.
<ul>
<li class="group">The first group</li>
<li>The first option</li>
<li>The second option</li>
<li>The third option</li>
<li class="group">The second group</li>
<li>Another option</li>
<li>One more option</li>
</ul>
Add selected
class to the desired item:
<ul>
<li>Now I'm not selected</li>
<li class="selected">I'll be selected!</li>
<li>Good luck another time</li>
</ul>
cccombo.list.filterItems(function(item, index) {
return (
item.element.dataset.foo > 0 &&
item.element.dataset.bar == 'baz'
);
});
change
event is dispatched to a button
or input
element,
depends on the type of Cccombo usage.
Please note, that change
event is not dispatched to the hidden input
,
added by Cccombo for button
type of usage.
Cccombo assign the cccombo
property with a Cccombo-instance value
to all initialized elements. So, you can call any Cccombo functions from
this property.
select()
can receive a String argument (value
of the desired item)
or an instance of CccomboItem
.
<div id="cccombo" class="cccombo">
<button type="button"></button>
<ul>
<li data-value="USD">$</li>
<li data-value="EUR">€</li>
<li data-value="RUB">₽</li>
</ul>
</div>
document.querySelector('#cccombo').cccombo.select('EUR');
<div class="cccombo">
<button type="button"></button>
<div class="dropdown">
<h4>List 1</h4>
<ul>
<li>The first option of the List 1</li>
<li>The second option of the List 1</li>
<li>The third option of the List 1</li>
</ul>
<h4>List 2</h4>
<ul>
<li>The first option of the List 2</li>
<li>The second option of the List 2</li>
<li>The third option of the List 2</li>
</ul>
</div>
</div>