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="shop-wrap">
  <h1>MY SHOPPING LIST</h1>
  
  <!-- (A) ADD NEW ITEM -->
  <form id="shop-form">
    <input type="text" id="shop-item" placeholder="Item Name" required disabled/>
    <input type="submit" id="shop-add" value="Add" disabled/>
  </form>

  <!-- (B) SHOPPING LIST -->
  <div id="shop-list"></div>
</div>

<!-- (X) VISIT CODE-BOXX -->
<div id="code-boxx">
  Visit
  <a href="https://code-boxx.com/shopping-list-vanilla-javascript/"
     target="_blank">
    Code Boxx
  </a> for more details.
</div>
              
            
!

CSS

              
                /* (A) SHARED */
#shop-wrap input {
  padding: 10px;
  border: 0;
}
#shop-wrap input[type=button], #shop-wrap input[type=submit] {
  cursor: pointer;
  color: #fff;
  background: #5a75d6;
}
#shop-form, .item-row {
  display: flex;
  align-items: stretch;
}
#shop-item, .item-name { flex-grow: 1; }

/* (B) WRAPPER */
#shop-wrap {
  max-width: 500px;
  padding: 15px;
  margin: 0 auto;
}

/* (C) SHOPPING LIST */
.item-row { margin-top: 10px; }
.item-name {
  padding: 10px;
  background: #fff;
}
.item-name.item-got { background: #f5fffa; }
.item-name.item-got:before {
  content: "\02713";
  margin-right: 5px;
  font-weight: bold;
  color: #00d036;
}
.item-del { background: #de1919 !important; }

/* [DOES NOT MATTER] */
/* PAGE & BODY */
* {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}
body {
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1603400521630-9f2de124b33b?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0MTY5ODQyNA&ixlib=rb-1.2.1&q=85);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

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

JS

              
                var slist = {
  // (A) INITIALIZE SHOPPING LIST
  items : [],   // current shopping list
  hform : null, // html add item <form>
  hitem : null, // html add item <input> field
  hadd : null,  // html add item submit button
  hlist : null, // html <div> shopping list
  init : () => {
    // (A1) GET HTML ELEMENTS
    slist.hform = document.getElementById("shop-form");
    slist.hitem = document.getElementById("shop-item");
    slist.hadd = document.getElementById("shop-add");
    slist.hlist = document.getElementById("shop-list");

    // (A2) "ACTIVATE" HTML ADD ITEM FORM
    slist.hitem.setAttribute("autocomplete", "off");
    slist.hform.onsubmit = slist.add;
    slist.hitem.disabled = false;
    slist.hadd.disabled = false;

    // (A3) RESTORE PREVIOUS SHOPPING LIST
    if (localStorage.items == undefined) { localStorage.items = "[]"; }
    slist.items = JSON.parse(localStorage.items);

    // (A4) DRAW HTML SHOPPING LIST
    slist.draw();
  },

  // (B) SAVE SHOPPING LIST INTO LOCAL STORAGE
  save : () => {
    if (localStorage.items == undefined) { localStorage.items = "[]"; }
    localStorage.items = JSON.stringify(slist.items);
  },

  // (C) ADD NEW ITEM TO THE LIST
  add : evt => {
    // (C1) PREVENT FORM SUBMIT
    evt.preventDefault();

    // (C2) ADD NEW ITEM TO LIST
    slist.items.push({
      name : slist.hitem.value, // item name
      done : false // true for "got it", false for "not yet"
    });
    slist.hitem.value = "";
    slist.save();

    // (C3) REDRAW HTML SHOPPING LIST
    slist.draw();
  },

  // (D) DELETE SELECTED ITEM
  delete : id => { if (confirm("Remove this item?")) {
    slist.items.splice(id, 1);
    slist.save();
    slist.draw();
  }},

  // (E) TOGGLE ITEM BETWEEN "GOT IT" OR "NOT YET"
  toggle : id => {
    slist.items[id].done = !slist.items[id].done;
    slist.save();
    slist.draw();
  },

  // (F) DRAW THE HTML SHOPPING LIST
  draw : () => {
    // (F1) RESET HTML LIST
    slist.hlist.innerHTML = "";

    // (F2) NO ITEMS
    if (slist.items.length == 0) {
      slist.hlist.innerHTML = "<div class='item-row item-name'>No items found.</div>";
    }

    // (F3) DRAW ITEMS
    else {
      for (let i in slist.items) {
        // ITEM ROW
        let row = document.createElement("div");
        row.className = "item-row";
        slist.hlist.appendChild(row);

        // ITEM NAME
        let name = document.createElement("div");
        name.innerHTML = slist.items[i].name;
        name.className = "item-name";
        if (slist.items[i].done) { name.classList.add("item-got"); }
        row.appendChild(name);

        // DELETE BUTTON
        let del = document.createElement("input");
        del.className = "item-del";
        del.type = "button";
        del.value = "Delete";;
        del.onclick = () => { slist.delete(i); };
        row.appendChild(del);

        // COMPLETED/NOT YET BUTTON
        let ok = document.createElement("input");
        ok.className = "item-ok";
        ok.type = "button";
        ok.value = slist.items[i].done ? "Not Yet" : "Got It";
        ok.onclick = () => { slist.toggle(i); };
        row.appendChild(ok);
      }
    }
  }
};
window.addEventListener("load", slist.init);
              
            
!
999px

Console