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">
  <h1>JS BOOKS LIST</h1>
  
  <!-- (A) BOOK LIST -->
  <div id="wrap">
    <div id="list"></div>
    <div id="dummy" onclick="bl.toggle(true)">ADD BOOK</div>
  </div>

  <!-- (B) BOOK FORM -->
  <div id="form"><form onsubmit="return bl.save()">
    <div id="fClose" onclick="bl.toggle(false)">X</div>
    <input type="hidden" id="fID">
    <label>Title</label>
    <input type="text" id="fTitle" required>
    <label>Author</label>
    <input type="text" id="fAuthor" required>
    <label>ISBN (optional)</label>
    <input type="text" id="fISBN">
    <input type="submit" value="Save">
  </form></div>
  
  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/simple-books-list-javascript/" target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) SHARED */
#dummy, .rDel, .rEdit, #fClose, #form input[type=submit] { cursor: pointer; }

/* (B) BOOKS LIST */
/* (B1) LIST WRAPPER */
#wrap {
  max-width: 500px;
  margin: 0 auto;
}

/* (B2) SHARED ROWS */
#dummy, .row {
  padding: 15px;
  border-radius: 10px;
}

/* (B2) ADD NEW BOOK */
#dummy {
  text-align: center;
  font-weight: 700;
  color: #7e7e7e;
  border: 2px dashed #aaa;
}

/* (B3) LIST ITEMS */
.row {
  display: flex;
  align-items: center;
  margin: 15px 0;
  background: #fff;
}
.rDel, .rTxt, .rEdit { padding: 5px; }
.rDel, .rEdit { font-size: 20px; }
.rDel { color: #9f1d1d; }
.rTxt { flex-grow: 1; }
.rTitle { font-weight: 700; }
.rAuthor {
  margin-top: 5px;
  color: #7e7e7e;
  font-size: 14px;
}

/* (C) BOOK FORM */
/* (C1) FORM WRAPPER */
#form {
  /* (C1-1) FULL PAGE */
  position: fixed;
  top: 0; left: 0; z-index: 999;
  width: 100vw; height: 100vh;
  background: rgba(0, 0, 0, 0.5);

  /* (C1-2) CENTERED */
  display: flex;
  justify-content: center;
  align-items: center;

  /* (C1-3) HIDDEN */
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s;
}

/* (C1-4) SHOW */
#form.show {
  opacity: 1;
  visibility: visible;
}

/* (C2) FORM */
#form form {
  width: 400px;
  padding: 20px;
  border-radius: 10px;
  background: #fff;
  position: relative;
}

/* (C3) FORM FIELDS */
#fClose {
  position: absolute;
  top: 0; right: 0;
  padding: 10px;
  font-weight: 700;
  color: #fff;
  background: #9f1d1d;
}
#form input, #form label {
  display: block;
  width: 100%;
}
#form input { padding: 10px; }
#form label { padding: 10px 0; }
#form input[type=submit] {
  margin-top: 20px;
  color: #fff;
  background: #2142bb;
  border: 0;
}

/* (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-1608476268141-cb7054ceb2da?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NTgzMTUwMTc&ixlib=rb-1.2.1&q=80);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

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

/* 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 bl = {
  // (A) INIT
  data : null, // books list
  hList : null, // html books list
  hForm : null, // html book form
  fID : null, fTitle : null, fAuthor : null, fISBN : null, // html form fields
  init : () => {
    // (A1) GET HTML ELEMENTS
    bl.hList = document.getElementById("list");
    bl.hForm = document.getElementById("form");
    bl.fID = document.getElementById("fID");
    bl.fTitle = document.getElementById("fTitle");
    bl.fAuthor = document.getElementById("fAuthor");
    bl.fISBN = document.getElementById("fISBN");

    // (A2) LOAD ENTRIES
    bl.data = localStorage.getItem("books");
    if (bl.data==null) { bl.data = []; }
    else { bl.data = JSON.parse(bl.data); }

    // (A3) DRAW ENTRIES
    bl.draw();
  },

  // (B) TOGGLE FORM
  toggle : id => {
    if (id===false) {
      bl.fID.value = "";
      bl.fTitle.value = "";
      bl.fAuthor.value = "";
      bl.fISBN.value = "";
      bl.hForm.classList.remove("show");
    } else {
      if (Number.isInteger(id)) {
        bl.fID.value = id;
        bl.fTitle.value = bl.data[id].t;
        bl.fAuthor.value = bl.data[id].a;
        bl.fISBN.value = bl.data[id].i;
      }
      bl.hForm.classList.add("show");
    }
  },

  // (C) DRAW BOOKS HTML
  draw : () => {
    let row;
    bl.hList.innerHTML = "";
    bl.data.forEach((book, i) => {
      row = document.createElement("div");
      row.className = "row";
      row.innerHTML = `<div class="rDel" onclick="bl.del(${i})">X</div>
      <div class="rTxt">
        <div class="rTitle">${book.t}${(book.i?" ("+book.i+")":"")}</div>
        <div class="rAuthor">${book.a}</div>
      </div>
      <div class="rEdit" onclick="bl.toggle(${i})">&#9998;</div>`;
      bl.hList.appendChild(row);
    });
  },

  // (D) SAVE BOOK
  save : () => {
    // (D1) GET DATA
    let data = {
      t : bl.fTitle.value,
      a : bl.fAuthor.value,
      i : bl.fISBN.value
    };

    // (D2) UPDATE DATA ARRAY
    if (bl.fID.value=="") { bl.data.push(data); }
    else { bl.data[parseInt(bl.fID.value)] = data; }
    localStorage.setItem("books", JSON.stringify(bl.data));

    // (D3) UPDATE HTML INTERFACE
    bl.toggle(false);
    bl.draw();
    return false;
  },

  // (E) DELETE BOOK
  del : id => { if (confirm("Delete book?")) {
    bl.data.splice(id, 1);
    localStorage.setItem("books", JSON.stringify(bl.data));
    bl.draw();
  }}
};
window.onload = bl.init;
              
            
!
999px

Console