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

              
                <body class="wrapper">
	<header class="layout-header-todo">
		<h1>Tasks</h1>
		<p id="tasks-counter" class="tasks-counter">0</p>
	</header>

	<ul role="list" id="tasks-list" class="tasks-list"></ul>

	<form id="form-add-task" class="l-stack-xxs">
		<label for="form-input" class="visually-hidden">Task name</label>
		<div class="l-input-button">
			<input id="form-input" type="text" name="input" placeholder="Task name" required>
			<button type="submit" name="submit">Add task</button>
		</div>
	</form>
</body>
              
            
!

CSS

              
                /* This pen is using external base CSS stylesheet(s) in the CSS Settings */

* {
	margin: 0;
}

body {
	display: grid;
	grid-auto-rows: auto 1fr auto;
	gap: var(--s-600);
	padding-block: var(--s-600);
}

header {
	display: flex;
	flex-wrap: wrap;
	justify-content: space-between;
	align-items: center;
	row-gap: var(--s-200);
	column-gap: var(--s-600);
	font-size: calc(var(--base-size) * 2.5);
	line-height: 1;
	font-weight: var(--fw-700);
}

header * {
	font-size: inherit;
	line-height: inherit;
	font-weight: inherit;
}

form input {
	inline-size: 100%;
}

.tasks-counter {
	padding: var(--padding-xxxs);
	color: var(--c-lk-1);
	border-radius: var(--border-radius);
}

.tasks-list > * + * {
	margin-block-start: var(--s-400);
	padding-block-start: var(--s-400);
	border-block-start: var(--bw-2) solid var(--c-bg-3);
}

.task-item {
	padding-inline: var(--padding-xxs);
}

.task-actions button {
	font-size: var(--s-300);
}

.completed {
	text-decoration: line-through;
	opacity: 0.5;
}

              
            
!

JS

              
                const formAddTask = document.querySelector("#form-add-task");
const input = formAddTask.elements["input"];
const submit = formAddTask.elements["submit"];
const tasksList = document.querySelector("#tasks-list");
const tasksCounter = document.querySelector("#tasks-counter");

const taskButtonComplete = {
	text: "Complete",
	action: "task-complete"
};
const taskButtonUncomplete = {
	text: "Uncomplete",
	action: "task-uncomplete"
};
const taskButtonEdit = {
	text: "Edit",
	action: "task-edit"
};
const taskButtonDuplicate = {
	text: "Duplicate",
	action: "task-duplicate"
};
const taskButtonDelete = {
	text: "Delete",
	action: "task-delete"
};

const taskButtons = [
	taskButtonComplete,
	taskButtonEdit,
	taskButtonDuplicate,
	taskButtonDelete
];

formAddTask.addEventListener("submit", (e) => {
	e.preventDefault();

	if (isValidInput(input)) {
		const taskName = input.value.trim();
		const taskItem = createTaskItem(taskName);
		tasksList.append(taskItem);
		updateTaskCount();
		input.value = "";
	}
});

tasksList.addEventListener("click", (e) => {
	const taskButtonClicked = e.target.closest("[data-element='task-button']");

	if (taskButtonClicked) {
		const taskItem = taskButtonClicked.closest("[data-element='task-item']");
		const taskContent = taskItem.querySelector("[data-element='task-content']");

		switch (taskButtonClicked.dataset.action) {
			case "task-complete": {
				completeTask(taskItem, taskContent);
				changeTaskButton(taskButtonClicked, taskButtonUncomplete);
				updateTaskCount();
				break;
			}
			case "task-uncomplete": {
				uncompleteTask(taskItem, taskContent);
				changeTaskButton(taskButtonClicked, taskButtonComplete);
				updateTaskCount();
				break;
			}
			case "task-edit": {
				editTask(taskContent);
				break;
			}
			case "task-duplicate": {
				const duplicatedTask = duplicateTask(taskItem);
				taskItem.after(duplicatedTask);
				updateTaskCount();
				break;
			}
			case "task-delete": {
				deleteTask(taskItem, taskContent);
				updateTaskCount();
				break;
			}
		}
	}
});

function isValidInput(input) {
	return input.value.trim() ? true : false;
}

function createTaskButton({ text, action }) {
	const button = document.createElement("button");
	button.textContent = text;
	button.dataset.element = "task-button";
	button.dataset.action = action;
	return button;
}

function createTaskItem(taskName) {
	// taskItem > taskContent + taskActions
	const taskItem = document.createElement("li");
	taskItem.dataset.element = "task-item";
	taskItem.dataset.completed = "false";
	taskItem.classList.add("task-item", "l-stack-xs");

	// taskContent > task name
	const taskContent = document.createElement("div");
	taskContent.textContent = taskName;
	taskContent.dataset.element = "task-content";
	taskItem.append(taskContent);

	// taskActions > buttons to Complete, Rename, Duplicate, Delete...
	const taskActions = document.createElement("div");
	taskActions.dataset.element = "task-actions";
	taskActions.classList.add("task-actions", "l-cluster-xs");
	taskButtons.forEach((taskButton) =>
		taskActions.append(createTaskButton(taskButton))
	);

	taskItem.append(taskActions);

	return taskItem;
}

function deleteTask(taskItem, currentTaskContent) {
	const currentTaskName = currentTaskContent.textContent;
	const confirmation = confirm(
		`Are you sure you want to delete "${currentTaskName}"?`
	);
	if (confirmation) taskItem.remove();
}

function duplicateTask(taskItem) {
	return taskItem.cloneNode(true);
}

function changeTaskButton(taskButtonClicked, { text, action }) {
	taskButtonClicked.textContent = text;
	taskButtonClicked.dataset.action = action;
}

function completeTask(taskItem, taskContent) {
	taskItem.dataset.completed = "true";
	taskContent.classList.add("completed");
}

function uncompleteTask(taskItem, taskContent) {
	taskItem.dataset.completed = "false";
	taskContent.classList.remove("completed");
}

function editTask(currentTaskContent) {
	const currentTaskName = currentTaskContent.textContent;
	let newTaskName = prompt(`Edit "${currentTaskName}":`);
	if (newTaskName !== null) {
		newTaskName = newTaskName.trim();
		if (newTaskName) {
			currentTaskContent.textContent = newTaskName;
		}
	}
}

function updateTaskCount() {
	tasksCounter.textContent = tasksList.querySelectorAll(
		"[data-completed='false']"
	).length;
}

              
            
!
999px

Console