css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <h1>Who should drive?</h1>

<div id="app" aria-live="polite"></div>

<form id="add-driver">
	<label class="screen-reader" for="potential-driver">Name of driver</label>
	<input id="potential-driver" placeholder="Name of driver...">
	<button>Add Driver</button>
<button id="pick-driver">Who's driving?</button>
<button id="clear">Clear Drivers</button>
              body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
	font-size: 112.5%;
	margin-left: auto;
	margin-right: auto;
	max-width: 40em;
	width: 88%;

 * Visually hide an element, but leave it available for screen readers
 * @link https://github.com/h5bp/html5-boilerplate/blob/master/dist/css/main.css
 * @link http://snook.ca/archives/html_and_css/hiding-content-for-accessibility
.screen-reader {
	border: 0;
	clip: rect(0 0 0 0);
	height: 1px;
	margin: -1px;
	overflow: hidden;
	padding: 0;
	position: absolute;
	white-space: nowrap;
	width: 1px;
// Data

var data = {
	drivers: [],
	selected: null

// Methods

// Create the markup string
var template = function () {

	// Create driver items
	var driverList = data.drivers.map(function (driver, index) {
		return '<li>' + driver + ' <button data-remove-driver="' + index + '">Remove Driver</button></li>';

	// Create page markup
	var html = '';

	if (driverList.length > 0) {
		html += '<ul>' + driverList.join('') + '</ul>';

	if (data.selected) {
		html += '<p><strong>' + data.selected + ' is driving.</strong></p>';

	return html;


// Emit a custom event
var emitEvent = function (elem, detail) {

	// Create a new event
	var event = new CustomEvent('render', {
		bubbles: true,
		cancelable: true,
		detail: detail || {}

	// Dispatch the event


// Render our template into the DOM
var render = function () {
	var app = document.querySelector('#app');
	if (!app) return;
	app.innerHTML = template();
	emitEvent(app, data);

// Shuffle our array of drivers and pick one
var shuffle = function (arr) {
	// Create a clone of the array so that we don't shuffle the original one
	var arrClone = arr.slice();
	// Shuffle the array
	return arrClone.sort(function() {
		return Math.random() - 0.5;

// Get drivers from localStorage
var getDriversFromLocalStorage = function () {
	var drivers = localStorage.getItem('whosDriving');
	if (drivers) {
		data.drivers = JSON.parse(drivers);

// Handle submit events
var submitHandler = function (event) {

	// Make sure it was our add driver form
	if (!event.target.matches('#add-driver')) return;

	// Prevent default form behavior

	// Get the field
	var driver = event.target.querySelector('#potential-driver');
	if (!driver || driver.value.length < 1) return;

	// Update our data and UI

	// Clear the field and refocus
	driver.value = '';


// Remove a driver from the list
var removeDriver = function (event) {
	var index = event.target.getAttribute('data-remove-driver');
	data.drivers.splice(index, 1);

// Pick a driver
var pickDriver = function () {
	var randomize = shuffle(data.drivers);
	data.selected = randomize[0];

// Clear all drivers from the list
var clearDrivers = function () {
	data.drivers = [];

// Handle click events
var clickHandler = function (event) {

	if (event.target.matches('[data-remove-driver]')) {

	if (event.target.matches('#pick-driver')) {

	if (event.target.matches('#clear')) {


// Save to localStorage on render
var renderHandler = function (event) {
	localStorage.setItem('whosDriving', JSON.stringify(event.detail.drivers));

// Inits and Event Listeners

// Event listeners
document.addEventListener('submit', submitHandler, false);
document.documentElement.addEventListener('click', clickHandler, false);
document.addEventListener('render', renderHandler, false);

// Initial render
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................