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>
  <div class="container">
    <div class="row">
      <form id="submit-todo">
        <input type="text" name="todo" autocomplete="off" class="todo-input" />
        <button id="new-todo">Add new</button>
      </form>
    </div>
    <div class="row">
      <p class="message">You have 0 tasks !</p>
    </div>
    <div class="column">
      <ul class="todo-list"></ul>
    </div>
  </div>
  <script src="main.js"></script>
</body>
              
            
!

CSS

              
                html,
body {
  box-sizing: border-box;
  height: 100%;
}

.container {
  width: 80%;
  display: flex;
  flex-flow: column;
  justify-content: center;
  align-items: center;
  align-content: flex-start;
  margin: 0 auto;
}

.row {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  width: 50%;
}

.column {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 50%;
}

.todo-list {
  display: block;
  list-style: none;
  padding-left: 0;
}

.list-item {
  display: flex;
  flex-direction: row;
  flex-basis: 100%;
  justify-content: space-between;
  padding: 0.4em 0.2em;
  margin: 0.2em;
  border: 1px solid#3498db;
}

.list-item span {
  overflow-wrap: anywhere;
}
button {
  cursor: pointer;
  border: none;
}

form {
  width: 100%;
  display: flex;
}

.delete-task {
  color: #c0392b;
  background-color: transparent;
}

#new-todo {
  background-color: #27ae60;
  color: #ecf0f1;
  border: none;
  border-radius: 0;
  padding: 0.5em;
  font-weight: 600;
}

.todo-input {
  transition: border;
  outline: none;
  border: 1px solid #2c3e50;
  border-radius: 0;
  width: 80%;
}

input:focus {
  border: 1px solid #27ae60;
  transition: 1s, ease-in;
}

.validation-red {
  border: 1px solid #e74c3c;
}

              
            
!

JS

              
                const container = document.querySelector(".container");
const todoForm = container.querySelector("#submit-todo");
const todoList = container.querySelector("ul.todo-list");
const message = container.querySelector(".message");

todoForm.addEventListener("submit", (e) => {
  // Prevent the default action of the form
  e.preventDefault();
  // Grab the input by it's name
  const todoText = e.target.querySelector('input[name="todo"]');
  // Grab the input's value
  const todoValue = todoText.value;

  if (!todoValue) {
    // There is no value, alert the user
    alert("Please add the value for todo!");
    // Add validation class to the input
    todoText.classList.add("validation-red");
    // Stops further execution
    return;
  }
  // Input had the validation class but now doesn't anymore
  if (todoText.classList.contains("validation-red")) {
    // We passed the validation this time so remove it
    todoText.classList.remove("validation-red");
  }
  // Create the template using of the list item
  const tpl = `
  <li class="list-item">
      <input type="checkbox" class="mark-done"/>
      <span>${todoValue}</span><button class="delete-task">Delete</button>
  </li>
  `;
  // Insert new list item after ul begins
  todoList.insertAdjacentHTML("afterbegin", tpl);
  // Update the task count
  updateTaskCount();
  // Create delete button events
  addDeleteEvents();
  // Add checkbox events
  addMarkCompletedEvents();
  // Reset the input field value
  todoText.value = "";
});

const addDeleteEvents = () => {
  container.querySelectorAll(".delete-task").forEach((button) => {
    button.addEventListener("click", (e) => {
      deleteTask(e.target);
      e.stopImmediatePropagation();
    });
  });
};

const addMarkCompletedEvents = () => {
  container.querySelectorAll(".mark-done").forEach((checkBox) => {
    checkBox.addEventListener("change", (e) => {
      markAsCompleted(e.target);
      e.stopImmediatePropagation();
    });
  });
};

const deleteTask = (button) => {
  let task = button.parentElement;
  let taskText = task.querySelector("span");
  task.remove();
  updateTaskCount();
  alert(`Task ${taskText.innerText} removed!`);
};

const markAsCompleted = (checkbox) => {
  let taskText = checkbox.parentElement.querySelector("span");
  let checked = checkbox.checked;
  if (checked) {
    taskText.style = "text-decoration:line-through;";
  } else {
    taskText.style = "text-decoration:unset;";
  }
};

const updateTaskCount = () => {
  let tasksCount = todoList.childElementCount;
  message.innerText = `You have ${tasksCount} ${
    tasksCount === 1 ? "task" : "tasks"
  } !`;
};

              
            
!
999px

Console