Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!--
<h2>Multi-select</h2>
<label id="multi-select-label" style="display:block;">Multi Select Dropdown</label>
<div id="multi-select" class="multi-select" tabindex="0" aria-labeledby="multi-select-label this-multi-select-label">
	<span id="this-multi-select-label" aria-label="">
		<label>Select a value</label>
		<span class="chevron">&lt;</span>
	</span>
	<ul>
		<li>
			<label>
				<input type="checkbox" name="selected" value="0"/>
				Item 1 Item 1 Item 1 Item 1 Item 1
			</label>
		</li>
		<li>
			<label>
				<input type="checkbox" name="selected" value="0"/>
				Item 1
			</label>
		</li>
		<li>
			<label>
				<input type="checkbox" name="selected" value="0"/>
				Item 1
			</label>
		</li>
		<li>
			<label>
				<input type="checkbox" name="selected" value="0"/>
				Item 1
			</label>
		</li>
	</ul>
</div>
-->

<h2>Multi-select Plugin</h2>
<label id="multi-select-plugin-label" style="display:block;">Multi Select Plugin Dropdown</label>
<div id="multi-select-plugin" aria-labeledby="multi-select-plugin-label">
	<span class="toggle">
		<label>Select a value</label>
		<span class="chevron">&lt;</span>
	</span>
	<ul>
		<li>
			<label>
				<input type="checkbox" name="selected" value="0"/>
				Item 1
			</label>
		</li>
		<li>
			<label>
				<input type="checkbox" name="selected" value="1"/>
				Item 2
			</label>
		</li>
		<li>
			<label>
				<input type="checkbox" name="selected" value="2"/>
				Item 3
			</label>
		</li>
		<li>
			<label>
				<input type="checkbox" name="selected" value="3"/>
				Item 4
			</label>
		</li>
	</ul>
</div>

<h2>HTML Select</h2>
<select>
	<option>Option 1</option>
	<option>Option 2</option>
	<option>Option 3</option>
	<option>Option 4</option>
	<option>Option 5</option>
</select>

<!--<h3>Log</h3>
<p></p>-->
              
            
!

CSS

              
                * {
	box-sizing: border-box;
}

.multi-select, .multi-select-plugin {	
	display: inline-block;	
	position: relative;
	
	> span {
		border: none;
		background: none;
		position: relative;
		padding: .25em .5em;
		padding-right: 1.5em;
		display: block;
		border: solid 1px #000;
		cursor: default;
		
		> .chevron {
			display: inline-block;
			transform: rotate(-90deg) scale(1, 2) translate(-50%, 0);
			font-weight: bold;
			font-size: .75em;
			position: absolute;
			top: .2em;
			right: .75em;
		}
	}
	
	> ul {
		position: absolute;
		list-style: none;
		padding: 0;
		margin: 0;
		left: 0;
		top: 100%;
		min-width: 100%;
		z-index: 1000;
		background: #fff;
		border: 1px solid rgba(0,0,0,.15);
		box-shadow: 0 6px 12px rgba(0,0,0,.175);
		display: none;
		max-height: 320px;
		overflow-x: hidden;
		overflow-y: auto;
		
		> li {
			white-space: nowrap;
			
			&.selected {
				> label {
					background-color: LightBlue;
				}
			}
			
			&.focused {
				> label {
					background-color: DodgerBlue;
				}
			}
			
			> label {
				padding: .25em .5em;
				display: block;
				
				&:focus, &:hover {
					background-color: DodgerBlue;
				}
			}
		}
	}
	
	&.in {
		> ul {
			display: block;
		}
	}
	
	&-backdrop {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 900;
	}
}
              
            
!

JS

              
                (function($){
	'use strict';
	
	const DataStatePropertyName = 'multiselect';
	const EventNamespace = '.multiselect';
	const PluginName = 'MultiSelect';
	
	var old = $.fn[PluginName];
	$.fn[PluginName] = plugin;
    $.fn[PluginName].Constructor = MultiSelect;
    $.fn[PluginName].noConflict = function () {
        $.fn[PluginName] = old;
        return this;
    };

    // Defaults
    $.fn[PluginName].defaults = {
        
    };
	
	// Static members
    $.fn[PluginName].EventNamespace = function () {
        return EventNamespace.replace(/^\./ig, '');
    };
    $.fn[PluginName].GetNamespacedEvents = function (eventsArray) {
        return getNamespacedEvents(eventsArray);
    };
	
	function getNamespacedEvents(eventsArray) {
        var event;
        var namespacedEvents = "";
        while (event = eventsArray.shift()) {
            namespacedEvents += event + EventNamespace + " ";
        }
        return namespacedEvents.replace(/\s+$/g, '');
    }
	
	function plugin(option) {
        this.each(function () {
            var $target = $(this);
            var multiSelect = $target.data(DataStatePropertyName);
            var options = (typeof option === typeof {} && option) || {};

            if (!multiSelect) {
                $target.data(DataStatePropertyName, multiSelect = new MultiSelect(this, options));
            }

            if (typeof option === typeof "") {
                if (!(option in multiSelect)) {
                    throw "MultiSelect does not contain a method named '" + option + "'";
                }
                return multiSelect[option]();
            }
        });
    }

    function MultiSelect(element, options) {
        this.$element = $(element);
        this.options = $.extend({}, $.fn[PluginName].defaults, options);
        this.destroyFns = [];
		
		this.$toggle = this.$element.children('.toggle');
		this.$toggle.attr('id', this.$element.attr('id') + 'multi-select-label');
		this.$backdrop = null;
		this.$allToggle = null;

        init.apply(this);
    }
	
	MultiSelect.prototype.open = open;
	MultiSelect.prototype.close = close;
	
	function init() {
		this.$element
		.addClass('multi-select')
		.attr('tabindex', 0);
		
        initAria.apply(this);
		initEvents.apply(this);
		updateLabel.apply(this);
		injectToggleAll.apply(this);
		
		this.destroyFns.push(function() {
			return '|'
		});
    }
	
	function injectToggleAll() {
		if(this.$allToggle && !this.$allToggle.parent()) {
			this.$allToggle = null;
		}
		
		this.$allToggle = $("<li><label><input type='checkbox'/>(all)</label><li>");
		
		this.$element
		.children('ul:first')
		.prepend(this.$allToggle);
	}
	
	function initAria() {
		this.$element
		.attr('role', 'combobox')
		.attr('aria-multiselect', true)
		.attr('aria-expanded', false)
		.attr('aria-haspopup', false)
		.attr('aria-labeledby', this.$element.attr("aria-labeledby") + " " + this.$toggle.attr('id'));
		
		this.$toggle
		.attr('aria-label', '');
	}
	
	function initEvents() {
		var that = this;
		this.$element
		.on(getNamespacedEvents(['click']), function($event) {	
			if($event.target !== that.$toggle[0] && !that.$toggle.has($event.target).length) {
				return;
			}			

			if($(this).hasClass('in')) {
				that.close();
			} else {
				that.open();
			}
		})
		.on(getNamespacedEvents(['keydown']), function($event) {
			var next = false;
			switch($event.keyCode) {
				case 13: 
					if($(this).hasClass('in')) {
						that.close();
					} else {
						that.open();
					}
					break;
				case 9:
					if($event.target !== that.$element[0]	) {
						$event.preventDefault();
					}
				case 27:
					that.close();
					break;
				case 40:
					next = true;
				case 38:
					var $items = $(this)
					.children("ul:first")
					.find(":input, button, a");

					var foundAt = $.inArray(document.activeElement, $items);				
					if(next && ++foundAt === $items.length) {
						foundAt = 0;
					} else if(!next && --foundAt < 0) {
						foundAt = $items.length - 1;
					}

					$($items[foundAt])
					.trigger('focus');
			}
		})
		.on(getNamespacedEvents(['focus']), 'a, button, :input', function() {
			$(this)
			.parents('li:last')
			.addClass('focused');
		})
		.on(getNamespacedEvents(['blur']), 'a, button, :input', function() {
			$(this)
			.parents('li:last')
			.removeClass('focused');
		})
		.on(getNamespacedEvents(['change']), ':checkbox', function() {
			if(that.$allToggle && $(this).is(that.$allToggle.find(':checkbox'))) {
				var allChecked = that.$allToggle
				.find(':checkbox')
				.prop("checked");
				
				that.$element
				.find(':checkbox')
				.not(that.$allToggle.find(":checkbox"))
				.each(function(){
					$(this).prop("checked", allChecked);
					$(this)
					.parents('li:last')
					.toggleClass('selected', $(this).prop('checked'));
				});
				
				updateLabel.apply(that);
				return;
			}
			
			$(this)
			.parents('li:last')
			.toggleClass('selected', $(this).prop('checked'));
			
			var checkboxes = that.$element
			.find(":checkbox")
			.not(that.$allToggle.find(":checkbox"))
			.filter(":checked");
			
			that.$allToggle.find(":checkbox").prop("checked", checkboxes.length === checkboxes.end().length);

			updateLabel.apply(that);
		})
		.on(getNamespacedEvents(['mouseover']), 'ul', function() {
			$(this)
			.children(".focused")
			.removeClass("focused");
		});
	}
	
	function updateLabel() {
		var pluralize = function(wordSingular, count) {
			if(count !== 1) {
				switch(true) {
					case /y$/.test(wordSingular):
						wordSingular = wordSingular.replace(/y$/, "ies");
					default:
						wordSingular = wordSingular + "s";
				}
			}			
			return wordSingular;
		}
		
		var $checkboxes = this.$element
		.find('ul :checkbox');
		
		var allCount = $checkboxes.length;
		var checkedCount = $checkboxes.filter(":checked").length
		var label = checkedCount + " " + pluralize("item", checkedCount) + " selected";
		
		this.$toggle
		.children("label")
		.text(checkedCount ? (checkedCount === allCount ? '(all)' : label) : 'Select a value');
		
		this.$element
		.children('ul')
		.attr("aria-label", label + " of " + allCount + " " + pluralize("item", allCount));
	}
	
	function ensureFocus() {
		this.$element
		.children("ul:first")
		.find(":input, button, a")
		.first()
		.trigger('focus')
		.end()
		.end()
		.find(":checked")
		.first()
		.trigger('focus');
	}
	
	function addBackdrop() {
		if(this.$backdrop) {
			return;
		}
		
		var that = this;
		this.$backdrop = $("<div class='multi-select-backdrop'/>");
		this.$element.append(this.$backdrop);
		
		this.$backdrop
		.on('click', function() {
			$(this)
			.off('click')
			.remove();
			
			that.$backdrop = null;			
			that.close();
		});
	}
	
	function open() {
		if(this.$element.hasClass('in')) {
			return;
		}

		this.$element
		.addClass('in');
		
		this.$element
		.attr('aria-expanded', true)
		.attr('aria-haspopup', true);

		addBackdrop.apply(this);
		//ensureFocus.apply(this);
	}
	
	function close() {
		this.$element
		.removeClass('in')
		.trigger('focus');
		
		this.$element
		.attr('aria-expanded', false)
		.attr('aria-haspopup', false);

		if(this.$backdrop) {
			this.$backdrop.trigger('click');
		}
	}	
})(jQuery);

$(document).ready(function(){
	$('#multi-select-plugin')
	.MultiSelect();
});

/*
$(document).ready(function(){
	var $backdrop = null;
	var $multiSelect = $('.multi-select');
	var $toggle = $multiSelect.children('span:first');
	
	initAria();
	
	$multiSelect
	.on('click', function($event) {	
		if($event.target !== $toggle[0] && !$toggle.has($event.target).length) {
			return;
		}
		
		if($(this).hasClass('in')) {
			close();
		} else {
			open();
		}
	})
	.on('keydown', function($event) {
		var next = false;
		switch($event.keyCode) {
			case 13: 
				open();
				break;
			case 9:
				if($event.target !== $multiSelect[0]	) {
					$event.preventDefault();
				}
			case 27:
				close();
				break;
			case 40:
				next = true;
			case 38:
				var $items = $(this)
				.children("ul:first")
				.find(":input, button, a");
				
				var foundAt = $.inArray(document.activeElement, $items);
				//if(foundAt < 0) {
				//	ensureFocus();
				//	return;
				//}
				
				if(next && ++foundAt === $items.length) {
					foundAt = 0;
				} else if(!next && --foundAt < 0) {
					foundAt = $items.length - 1;
				}
				
				$($items[foundAt])
				.trigger('focus');
		}
	})
	.on('focus', 'a, button, :input', function() {
		$(this)
		.parents('li:last')
		.addClass('focused');
	})
	.on('blur', 'a, button, :input', function() {
		$(this)
		.parents('li:last')
		.removeClass('focused');
	})
	.on('change', ':checkbox', function() {
		$(this)
		.parents('li:last')
		.toggleClass('selected', $(this).prop('checked'));
		
		updateLabel();
	})
	.on('mouseover', 'ul', function() {
		$(this)
		.children(".focused")
		.removeClass("focused");
	});
	
	function updateLabel() {
		var $checkboxes = $multiSelect
		.find(':checkbox');
		
		var allCount = $checkboxes.length;
		var checkedCount = $checkboxes.filter(":checked").length
		var label = checkedCount + " items selected";
		
		$toggle
		.children("label")
		.text(checkedCount ? (checkedCount === allCount ? '(all)' : label) : 'Select a value');
		
		$multiSelect
		.children('ul')
		.attr("aria-label", label + " of " + allCount + " items");
	}
	
	function initAria() {
		$multiSelect
		.attr('role', 'combobox')
		.attr('aria-multiselect', true)
		.attr('aria-expanded', false)
		.attr('aria-haspopup', false);
	}
	
	function ensureFocus() {
		$multiSelect
		.children("ul:first")
		.find(":input, button, a")
		.first()
		.trigger('focus')
		.end()
		.end()
		.find(":checked")
		.first()
		.trigger('focus');
	}
	
	function addBackdrop() {
		if($backdrop) {
			return;
		}
		
		$backdrop = $("<div class='multi-select-backdrop'/>");
		$multiSelect.append($backdrop);
		
		$backdrop
		.on('click', function() {
			$(this)
			.off('click')
			.remove();
			
			$backdrop = null;
			
			close();
		});
	}
	
	function open() {
		if($multiSelect.hasClass('in')) {
			return;
		}

		$multiSelect
		.addClass('in');
		
		$multiSelect
		.attr('aria-expanded', true)
		.attr('aria-haspopup', true);

		addBackdrop();
		ensureFocus();
	}
	
	function close() {
		$multiSelect
		.removeClass('in')
		//.find(">button:first")
		.trigger('focus');
		
		$multiSelect
		.attr('aria-expanded', false)
		.attr('aria-haspopup', false);

		if($backdrop) {
			$backdrop.trigger('click');
		}
	}
});
*/
              
            
!
999px

Console