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

              
                <div id="todo-wrap" class="widget-wrap">
  <h1>TO DO LIST</h1>

  <!-- (A) ADD NEW ITEM -->
  <form id="todo-add">
    <input type="text" id="todo-item" placeholder="New Item" required>
    <input type="submit" id="todo-save" value="&#10010;">
  </form>

  <!-- (B) LIST ITEMS -->
  <div id="todo-list"></div>

  <!-- (C) ITEM TEMPLATE -->
  <template id="todo-template">
    <div class="todo-row">
      <input type="button" class="todo-del" value="&#10006;">
      <input type="button" class="todo-edit" value="&#9998;">
      <input type="text" class="todo-item" disabled>
      <input type="button" class="todo-done" value="&#10004;">
    </div>
  </template>
  
  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/simple-javascript-to-do-list/"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>

              
            
!

CSS

              
                /* (A) PAGE */
* {
  font-family: Arial, Helvetica, sans-serif;
  box-sizing: border-box;
}

/* (B) WRAPPER */
.widget-wrap {
  min-width: 600px;
  padding: 30px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.3);
}

/* (C) SHARED */
#todo-wrap input, .todo-item {
  border: 0;
  padding: 10px;
  font-size: 16px;
  margin-bottom: 10px;
}

/* (D) HEADER */
#todo-wrap h1 {
  color: #fff;
  margin: 20px 0;
}

/* (E) ROW ITEMS */
#todo-add, .todo-row {
  display: flex;
  align-items: stretch;
}
#todo-item, .todo-item {
  flex-grow: 1;
  background: #fff;
}
.todo-item.todo-ok { background: #cffff4; }
.todo-item.editing { background: #fffdcf; }
#todo-save, .todo-del, .todo-edit, .todo-done {
  color: #fff;
  cursor: pointer;
}
.todo-del { background: #ed3636; }
#todo-save, .todo-edit, .todo-done { background: #2781cf; }

/* (X) DOES NOT MATTER QUITE MATTER - COSMETICS */
body {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1517873569652-f231d7134909?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0MTY5OTcyMQ&ixlib=rb-1.2.1&q=85);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

/* FOOTER */
#code-boxx {
  font-weight: 600;
  margin-top: 30px;
  color: #fff;
}
#code-boxx a {
  display: inline-block;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
              
            
!

JS

              
                var todo = {
  // (A) INITIALIZE TO DO LIST
  data : [],        // todo list data array
  hAdd : null,      // html add item text field
  hTemplate : null, // html item row template
  hList : null,     // html to do list
  init : () => {
    // (A1) INIT LOCALSTORAGE
    if (localStorage.todo == undefined) { localStorage.todo = "[]"; }

    // (A2) RESTORE PREVIOUS SESSION
    todo.data = JSON.parse(localStorage.todo);

    // (A3) GET HTML ELEMENTS
    todo.hAdd = document.getElementById("todo-item");
    todo.hTemplate = document.getElementById("todo-template").content;
    todo.hList = document.getElementById("todo-list");

    // (A4) "ENABLE" ADD ITEM FORM
    document.getElementById("todo-add").onsubmit = todo.add;

    // (A5) DRAW TO DO LIST
    todo.draw();
  },

  // (B) DRAW TO DO LIST
  draw : () => {
    // (B1) RESET LIST
    todo.hList.innerHTML = "";

    // (B2) LOOP & GENERATE ROWS
    if (todo.data.length>0) { for (let id in todo.data) {
      let row = todo.hTemplate.cloneNode(true),
          edit = row.querySelector(".todo-edit"),
          item = row.querySelector(".todo-item");
      item.value = todo.data[id][0];
      item.id = "item" + id;
      edit.id = "edit" + id;
      edit.onclick = () => todo.edit(id);
      row.querySelector(".todo-done").onclick = () => todo.toggle(id);
      row.querySelector(".todo-del").onclick = () => todo.del(id);
      if (todo.data[id][1]) { row.querySelector(".todo-item").classList.add("todo-ok"); }
      todo.hList.appendChild(row);
    }}
  },

  // (C) HELPER - SAVE DATA INTO LOCAL STORAGE
  save: () => {
    localStorage.todo = JSON.stringify(todo.data);
    todo.draw();
  },

  // (D) ADD A NEW ITEM TO THE LIST
  add : () => {
    todo.data.push([todo.hAdd.value, false]);
    todo.hAdd.value = "";
    todo.save();
    return false;
  },

  // (E) TOGGLE EDIT ITEM
  edit : id => {
    // (E1) GET EDIT BUTTON + ITEM
    let edit = document.getElementById("edit" + id),
        item = document.getElementById("item" + id);

    // (E2) SET EDITABLE
    if (item.disabled) {
      item.classList.add("editing");
      item.disabled = false;
      item.select();
      edit.value = "\u270F";
    }

    // (E3) SAVE
    else {
      item.classList.remove("editing");
      item.disabled = true;
      edit.value = "\u270E";
      todo.data[id][0] = item.value;
      todo.save();
    }
  },

  // (F) UPDATE TODO ITEM DONE/NOT YET
  toggle: id => {
    todo.data[id][1] = !todo.data[id][1];
    todo.save();
  },

  // (G) DELETE ITEM
  del: id => { if (confirm("Delete task?")) {
    todo.data.splice(id, 1);
    todo.save();
  }}
};

// (H) PAGE INIT
window.addEventListener("load", todo.init);
              
            
!
999px

Console