Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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">
  <h1>EXPENSE LIST</h1>
  
  <!-- (A) ADD NEW RECORD -->
  <form id="add_form" onsubmit="return track.add()">
    <input id="add_date" type="date" required/>
    <input id="add_name" type="text" placeholder="Item Name" required/>
    <input id="add_amt" type="number" min="1" step="0.01" placeholder="Amount" value="1.00" required/>
    <input type="submit" value="Add"/>
  </form>

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

CSS

              
                /* (A) ENTIRE PAGE */
form, input, select { padding: 10px; }
input[type=submit], input[type=button] {
  padding: 10px 20px;
  color: #fff;
  border: 0;
  background: #933333;
  cursor: pointer;
}

/* (B) ADD ITEM */
#add_form {
  margin-bottom: 5px;
  display: flex;
  border: 1px solid #eee;
  background: #f2f2f2;
}
#add_date {}
#add_name { flex-grow: 1; }
#add_amt { width: 100px; }

/* (C) RECORDS */
#rec_wrap {}
#rec_wrap .row {
  margin-bottom: 5px;
  display: flex;
  align-items: center;
  border: 1px solid #ddd;
  background: #ededed;
}
#rec_wrap .row:nth-child(even) { background: #fff; }
#rec_wrap .row.total {
  font-weight: 700;
  padding: 10px;
  border: 1px solid #f00;
  background: #fff3f3;
}
#rec_wrap .row div { padding: 0 10px; }
#rec_wrap .date {}
#rec_wrap .name { flex-grow: 1; }
#rec_wrap .amt {}

/* (X) DOES NOT MATTER */
/* PAGE & BODY */
* {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  background: url(https://images.unsplash.com/photo-1643101809653-45c4121a3934?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NTYzOTU0ODg&ixlib=rb-1.2.1&q=80);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

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

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

JS

              
                var track = {
  // (A) PROPERTIES
  data  : {},    // expenses data
  hDate : null,  // html add item date
  hName : null,  // html add item name
  hAmt : null,   // html add item amount
  hWrap : null,  // html records wrapper

  // (B) SUPPORT FUNCTION - SAVE TO LOCAL STORAGE
  save : () => {
    localStorage.setItem("expenses", JSON.stringify(track.data));
  },

  // (C) INIT
  init : () => {
    // (C1) GET HTML ELEMENTS
    track.hDate = document.getElementById("add_date");
    track.hName = document.getElementById("add_name");
    track.hAmt = document.getElementById("add_amt");
    track.hWrap = document.getElementById("rec_wrap");

    // (C2) DEFAULT TODAY'S DATE
    track.hDate.valueAsDate  = new Date();

    // (C3) LOAD RECORDS FROM LOCAL STORAGE
    let data = localStorage.getItem("expenses");
    if (data != null) { track.data = JSON.parse(data); }

    // (C4) DRAW RECORDS
    track.draw();
  },

  // (D) DRAW RECORDS
  draw : () => {
    // (D1) EMPTY WRAPPER
    track.hWrap.innerHTML = "";

    // (D2) LOOP & DRAW RECORDS
    let row, total = 0;
    for (let date in track.data) { for (let i in track.data[date]) {
      let entry = track.data[date][i];
      total += parseFloat(entry[1]);

      row = document.createElement("div");
      row.className = "row";
      row.innerHTML = `
        <input type="button" value="X" onclick="track.del('${date}', ${i})">
        <div class="name">[${date}] ${entry[0]}</div>
        <div class="amt">$${entry[1]}</div>`;
      track.hWrap.appendChild(row);
    }}

    // (D3) TOTAL
    row = document.createElement("div");
    row.className = "row total";
    row.innerHTML = `<div class="name">Total Expenses</div><div class="amt">$${total.toFixed(2)}</div>`
    track.hWrap.appendChild(row);
  },

  // (E) ADD ENTRY
  add : () => {
    // (E1) PUSH NEW ENTRY
    if (track.data[track.hDate.value]==undefined) { track.data[track.hDate.value] = []; }
    track.data[track.hDate.value].push([track.hName.value, track.hAmt.value]);

    // (E2) SORT & SAVE TO LOCALSTORAGE
    // CREDITS : https://www.w3docs.com/snippets/javascript/how-to-sort-javascript-object-by-key.html
    track.data = Object.keys(track.data).sort().reduce((r,k) => (r[k] = track.data[k], r), {});
    track.save();

    // (E3) RESET FORM
    track.hName.value = "";
    track.hAmt.value = "1.00";

    // (E4) DRAW RECORDS
    track.draw();
    return false;
  },

  // (F) REMOVE ENTRY
  del : (date, i) => { if (confirm("Delete Entry?")) {
    track.data[date].splice(i, 1);
    if (track.data[date].length==0) { delete track.data[date]; }
    track.save();
    track.draw();
  }}
};
window.addEventListener("DOMContentLoaded", track.init);

              
            
!
999px

Console