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 class="widget-wrap">
  <div id="cart"></div>
  <h1>GROCERY LIST</h1>
  
  <!-- (A) ADD ITEM -->
  <form id="gro-form">
    <input type="number" id="gro-qty" min="1" value="1" placeholder="Quantity" required disabled>
    <input type="text" id="gro-item" placeholder="Item Name" required disabled>
    <button id="gro-add" class="material-icons" disabled>add</button>
  </form>

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

CSS

              
                /* (A) ADD ITEM */
#gro-form, input, button {
  font-size: 16px;
  padding: 10px;
}
#gro-form {
  display: flex;
  border: 1px solid #ccc;
  background: #f2f2f2;
}
button, input { border: 0; }
input[type=number] { width: 80px; }
input[type=text] { flex-grow: 1; }
button, input[type=button], input[type=submit] {
  cursor: pointer;
  color: #fff;
  background: #b10b1a;
}

/* (B) ITEMS LIST */
#gro-list { margin-top: 20px; }
.item-row {
  margin: 10px 0;
  display: flex;
  align-items: center;
}
.item-row.yes { background: #c6ffc3; }
.item-row.no { background: #ffd7d7; }
.item-name {
  flex-grow: 1;
  padding: 0 10px;
}
.item-ok {
  color: #000;
  background: none;
}

/* (X) DOES NOT MATTER */
/* PAGE & BODY */
* {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}
body {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1506976785307-8732e854ad03?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NTU4ODAyNzE&ixlib=rb-1.2.1&q=80);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

/* WIDGET */
.widget-wrap {
  min-width: 600px;
  padding: 30px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.8);
}

/* SVG */
#cart {
  width: 100px; height:100px;
  background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 576 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M528.12 301.319l47.273-208C578.806 78.301 567.391 64 551.99 64H159.208l-9.166-44.81C147.758 8.021 137.93 0 126.529 0H24C10.745 0 0 10.745 0 24v16c0 13.255 10.745 24 24 24h69.883l70.248 343.435C147.325 417.1 136 435.222 136 456c0 30.928 25.072 56 56 56s56-25.072 56-56c0-15.674-6.447-29.835-16.824-40h209.647C430.447 426.165 424 440.326 424 456c0 30.928 25.072 56 56 56s56-25.072 56-56c0-22.172-12.888-41.332-31.579-50.405l5.517-24.276c3.413-15.018-8.002-29.319-23.403-29.319H218.117l-6.545-32h293.145c11.206 0 20.92-7.754 23.403-18.681z" /></svg>');
  background-repeat: no-repeat;
  background-position: center;
}

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

JS

              
                var glist = {
  // (A) INITIALIZE GROCERY LIST
  items : [],   // current grocery list
  hqty : null,  // html add quantity field
  hitem : null, // html add item field
  hlist : null, // html <div> grocery list
  init : () => {
    // (A1) GET HTML ELEMENTS
    glist.hqty = document.getElementById("gro-qty");
    glist.hitem = document.getElementById("gro-item");
    glist.hlist = document.getElementById("gro-list");

    // (A2) ENABLE FORM
    glist.hqty.disabled = false;
    glist.hitem.disabled = false;
    document.getElementById("gro-add").disabled = false;
    document.getElementById("gro-form").onsubmit = glist.add;

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

    // (A4) DRAW HTML GROCERY LIST
    glist.draw();
  },

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

  // (C) ADD NEW ITEM TO THE LIST
  add : evt => {
    // (C1) ADD NEW ITEM TO LIST
    glist.items.push({
      qty  : glist.hqty.value,  // item quantity
      name : glist.hitem.value, // item name
      got  : false              // item accquired
    });
    glist.save();

    // (C2) RESET HTML ADD FORM
    glist.hqty.value = 1;
    glist.hitem.value = "";

    // (C3) REDRAW HTML GROCERY LIST
    glist.draw();
    return false;
  },

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

  // (E) TOGGLE ITEM BETWEEN "ACCQUIRED" & "NOT YET"
  toggle : id => {
    glist.items[id].got = !glist.items[id].got;
    glist.save();
    glist.draw();
  },

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

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

    // (F3) DRAW ITEMS
    else { for (let i in glist.items) {
      // (F3-1) ITEM ROW
      let row = document.createElement("div");
      row.className = glist.items[i].got ? "item-row yes" : "item-row no";
      glist.hlist.appendChild(row);

      // (F3-2) DELETE BUTTON
      let del = document.createElement("button");
      del.className = "item-del material-icons";
      del.innerHTML = "delete";
      del.onclick = () => glist.delete(i);
      row.appendChild(del);

      // (F3-3) ITEM QUANTITY & NAME
      let name = document.createElement("div");
      name.innerHTML = `${glist.items[i].qty} X ${glist.items[i].name}`;
      name.className = "item-name";
      row.appendChild(name);

      // (F3-4) ITEM ACCQUIRED
      let ok = document.createElement("button");
      ok.className = "item-ok material-icons";
      ok.innerHTML = glist.items[i].got ? "done" : "clear";
      ok.onclick = () => glist.toggle(i);
      row.appendChild(ok);
    }}
  }
};
window.addEventListener("load", glist.init);
              
            
!
999px

Console