cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <div class="container">
	<h1>TO-DO LIST</h1>
	<p>
		<label for="new-task">Add Item</label><input id="new-task" type="text"><button id="add-task">Add</button>
	</p>

	<h3>Todo</h3>
	<ul id="incomplete-tasks">
		<li><input type="checkbox"><label>Pay Bills</label><input type="text"><button class="edit">Edit</button><button class="delete">Delete</button></li>
		<li><input type="checkbox"><label>Go Shopping</label><input type="text" value="Go Shopping"><button class="edit">Edit</button><button class="delete">Delete</button></li>

	</ul>

	<h3>Completed</h3>
	<ul id="completed-tasks">
		<li><input type="checkbox" checked><label>See the Doctor</label><input type="text"><button class="edit">Edit</button><button class="delete">Delete</button></li>
	</ul>
</div>
            
          
!
            
              body {
  background: #fff;
  color: #333;
  font-family: Lato, sans-serif;
}

h1 {
	text-align: center;
}

.container {
  display: block;
  width: 401px;
  margin: 100px auto 0;
}
ul {
  margin: 0;
  padding: 0;
}
li * {
  float: left;
}
li, h3 {
  clear:both;
  list-style:none;
}
input, button {
  outline: none;
}
button {
  background: none;
  border: 0px;
  color: #888;
  font-size: 15px;
  width: 60px;
  margin: 10px 0 0;
  font-family: Lato, sans-serif;
  cursor: pointer;
}
button:hover {
  color: #333;
}

h3,
label[for='new-task'] {
  color: #333;
  font-weight: 700;
  font-size: 15px;
  border-bottom: 2px solid #333;
  padding: 30px 0 10px;
  margin: 0;
  text-transform: uppercase;
}
input[type="text"] {
  margin: 0;
  font-size: 18px;
  line-height: 18px;
  height: 18px;
  padding: 10px;
  border: 1px solid #ddd;
  background: #fff;
  border-radius: 6px;
  font-family: Lato, sans-serif;
  color: #888;
}
input[type="text"]:focus {
  color: #333;
}

label[for='new-task'] {
  display: block;
  margin: 0 0 20px;
}
input#new-task {
  float: left;
  width: 318px;
}
p > button:hover {
  color: #0FC57C;
}

li {
  overflow: hidden;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
}
li > input[type="checkbox"] {
  margin: 0 10px;
  position: relative;
  top: 15px;
}
li > label {
  font-size: 18px;
  line-height: 40px;
  width: 237px;
  padding: 0 0 0 11px;
}
li >  input[type="text"] {
  width: 226px;
}
li > .delete:hover {
  color: #CF2323;
}

#completed-tasks label {
  text-decoration: line-through;
  color: #888;
}

ul li input[type=text] {
  display:none;
}

ul li.editMode input[type=text] {
  display:block;
}

ul li.editMode label {
  display:none;
}
            
          
!
            
              window.addEventListener("load", init, false);

function getId(id)
{
	return document.getElementById(id);
}

function init()
{
	//element variables
	var taskInput, addTaskBtn, incompleteTasks, completedTasks;
	
	//function variables
	var createTask, addTask, editTask, deleteTask, taskCompleted, taskIncomplete;
	
	taskInput = getId("new-task");
	addTaskBtn = getId("add-task");
	incompleteTasks = getId("incomplete-tasks");
	completedTasks = getId("completed-tasks");
	
	createTask = function()
	{
		if(taskInput.value == "")
			{
				alert("Enter some text to create a to-do item");
			}
		else
			{
				var newListItem, newCheckbox, newLabel, newInput, newEditButton, newDeleteButton;
				
				newListItem = document.createElement("li");
				newCheckbox = document.createElement("input");
				newLabel = document.createElement("label");
				newInput = document.createElement("input");
				newEditButton = document.createElement("button");
				newDeleteButton = document.createElement("button");
				
				newCheckbox.setAttribute("type", "checkbox");
				newInput.setAttribute("type", "text");
				newLabel.innerHTML = taskInput.value;
				newInput.value = taskInput.value;
				newEditButton.classList.add("edit");
				newEditButton.innerHTML = "Edit";
				newDeleteButton.classList.add("delete");
				newDeleteButton.innerHTML = "Delete";
				
				newListItem.appendChild(newCheckbox);
				newListItem.appendChild(newLabel);
				newListItem.appendChild(newInput);
				newListItem.appendChild(newEditButton);
				newListItem.appendChild(newDeleteButton);
				
				return newListItem;
			}
	}
	
	addTask = function()
	{
		var newTaskItem = createTask();
		incompleteTasks.appendChild(newTaskItem);
		
		//bind events to new list entry
		bindTaskButtons(newTaskItem, taskCompleted);
		
		taskInput.value = "";
		taskInput.focus();
	}
	
	editTask = function()
	{
		var listItem = this.parentElement;
		var input = listItem.querySelector("input[type=text]");
		var label = listItem.querySelector("label");
		var editButton = listItem.querySelector("button.edit");
		
		if(listItem.classList.contains("editMode"))
			{
				label.innerHTML = input.value;
				editButton.innerHTML = "Edit";
			}
		else
			{
				input.value = label.innerHTML;
				editButton.innerHTML = "Done";
			}
		
		listItem.classList.toggle("editMode");
		
	}
	
	deleteTask = function()
	{
		var confirmDelete = confirm("Delete this task?");
		
		if(confirmDelete == true)
		{
			var listItem, list;
			listItem = this.parentElement;
			list = listItem.parentElement;
			list.removeChild(listItem);
		}
	}
	
	taskCompleted = function()
	{
		var listItem = this.parentElement;
		completedTasks.appendChild(listItem);
		listItem.querySelector("input[type=checkbox]").removeEventListener("change", taskCompleted);
		bindTaskButtons(listItem, taskIncomplete);
	}
	
	taskIncomplete = function()
	{
		var listItem = this.parentElement;
		incompleteTasks.appendChild(listItem);
		listItem.querySelector("input[type=checkbox]").removeEventListener("change", taskIncomplete);
		bindTaskButtons(listItem, taskCompleted);
	}
	
	addTaskBtn.addEventListener("click", addTask, false);
	
	//automate binding to list's children elements
	//"checkboxHandler" parameter, since the checkboxes
	//receive different functions for complete or incomplete lists
	var bindTaskButtons = function(listItem, checkboxHandler)
	{
		var checkbox, editButton, deleteButton;
		checkbox = listItem.querySelector("input[type=checkbox]");
		editButton = listItem.querySelector("button.edit");
		deleteButton = listItem.querySelector("button.delete");
		
		checkbox.addEventListener("change", checkboxHandler);
		editButton.addEventListener("click", editTask, false);
		deleteButton.addEventListener("click", deleteTask, false);
	}
	
	for(var i = 0; i < incompleteTasks.children.length; i++)
	{
		bindTaskButtons(incompleteTasks.children[i], taskCompleted);
	}
	
	for(var i = 0; i < incompleteTasks.children.length; i++)
	{
		bindTaskButtons(completedTasks.children[i], taskIncomplete);
	}
}/*init*/
            
          
!
999px
Loading ..................

Console