Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <h1>Todos</h1>

<form id="add-todo">
	<label>What do you want to do?</label>
	<input type="text" id="todo">
	<button>Add Todo</button>
</form>

<div id="app"></div>
              
            
!

CSS

              
                body {
	margin: 1em auto;
	width: 88%;
	max-width: 40em;
}
              
            
!

JS

              
                // DOM elements
let app = document.querySelector('#app');
let form = document.querySelector('#add-todo');

// Todo data
let todos = JSON.parse(localStorage.getItem('todos-state-based')) || [];

/**
 * Add todo to the list
 * @param {Event} event The event object
 */
function addTodo (event) {

	// Stop the form from reloading the page
	event.preventDefault();

	// If there's no field value, ignore the submission
	if (!form.todo.value) return;

	// Otherwise, add todo and rerender the UI
	todos.push(form.todo.value);
	app.innerHTML = getHTML();

	// Clear the form field
	form.todo.value = '';

	// Save the list
	localStorage.setItem('todos-state-based', JSON.stringify(todos));

}

/**
 * Remove todo items
 * @param  {Event} event The event object
 */
function removeTodo (event) {

	// Only run on [data-delete] items
	let index = event.target.getAttribute('data-delete');
	if (!index) return;

	// Otherwise, remove the todo and rerender the UI
	todos.splice(index, 1);
	app.innerHTML = getHTML();

	// Save the list
	localStorage.setItem('todos-state-based', JSON.stringify(todos));

}

/**
 * Create the HTML based on the app state
 */
function getHTML () {

	// If there are no todos, show a message
	if (!todos.length) {
		return `<p><em>You don't have any todos yet.</em></p>`;
	}

	// Otherwise, render the todo items
	return `
		<ul>
			${todos.map(function (todo, index) {
				return `<li>${todo} <button data-delete="${index}">Delete</button></li>`;
			}).join('')}
		</ul>`;

}

// Add todos when form is submitted
form.addEventListener('submit', addTodo);

// Remove todos when delete button is clicked
document.addEventListener('click', removeTodo);

// Render the UI
app.innerHTML = getHTML();
              
            
!
999px

Console