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

              
                <!-- (A) PERIOD SELECTOR -->
<div id="calPeriod">
  <select id="calMonth"></select>
  <input type="number" id="calYear">
</div>

<!-- (B) CALENDAR -->
<div id="calWrap"></div>

<!-- (C) EVENT FORM -->
<dialog id="calForm"><form method="dialog">
  <div id="evtClose">X</div>
  <h2>CALENDAR EVENT</h2>
  <label>Date</label>
  <input type="text" id="evtDate" readonly>
  <label>Details</label>
  <textarea id="evtTxt" required></textarea>

  <input id="evtDel" type="button" value="Delete">
  <input id="evtSave" type="submit" value="Save">
</form></dialog>

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

CSS

              
                /* (A) ENTIRE PAGE */
* {
  font-family: Arial, Helvetica, sans-serif;
  box-sizing: border-box;
}
body {
  position: relative;
  padding: 10px; margin: 0;
  overflow-x: hidden;
}
:root {
  --bg-color-a: #3b39af; /* page header + form label */
  --bg-color-b: #404040; /* calendar header */
  --bg-color-c: #d73a3a; /* buttons */
}
.hide { display: none !important; }

/* (B) PERIOD SELECTOR */
#calPeriod {
  padding: 15px;
  background: var(--bg-color-a);
}
#calPeriod input, #calPeriod select {
  font-size: 24px;
  border: 0; outline: none;
  color: #fff; background: 0;
  cursor: pointer;
}
#calPeriod option { color: #000; }
#calYear {
  width: 100px;
  margin-left: 10px;
}

/* (C) CALENDAR */
/* (C1) SHARED */
.calHead, .calRow { display: flex; }
.calCell { width: 14.2857%; }

/* (C2) HEADER - DAY NAMES */
.calHead .calCell {
  color: #fff; background: var(--bg-color-b);
  font-weight: 700; text-align: center;
  padding: 15px 0;
}

/* (C3) BODY - INDIVIDUAL DAYS */
.calBlank { background: #eee; }
.calToday { background: #feffd3; }
.calToday .cellDate { color: #ff9b9b; }
.calBody .calCell {
  border: 1px solid #f5f5f5;
  min-height: 80px;
}
.cellDate, .evt { padding: 5px; }
.cellDate {
  font-size: 14px;
  font-weight: 700;
  color: #b5b5b5;
}
.evt {
  font-size: 14px;
  white-space: nowrap; text-overflow: ellipsis;
  overflow: hidden;
  cursor: pointer;
}

/* (D) EVENT FORM */
#calForm {
  position: absolute; top: 0; left: 0; z-index: 999;
  width: 100vw; height: 100%; min-height: 100vh;
  padding: 0; margin: 0; border: 0;
  background: rgba(0,0,0,0.5);
}
#calForm:is([open]) {
  display: flex; flex-wrap: wrap;
  align-items: center; justify-content: center;
}
#calForm form {
  position: relative;
  width: 450px; padding: 15px;
  border-radius: 10px;
  background: #fff;
}
#evtClose {
  position: absolute;
  top: 0; right: 0;
  padding: 10px;
  color: #b12020;
  font-size: 28px;
  font-weight: 700;
  cursor: pointer;
}
#calForm h2 { margin: 0 0 15px 0; }
#calForm label, #calForm input[type=text], #calForm textarea { width: 100%; }
#calForm label {
  color: #a1a1a1;
  font-weight: 700;
  font-size: 12px;
  margin-bottom: 5px;
}
#calForm input[type=text], #calForm textarea {
  width: 100%;
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid #eee;
  background: #f5f5f5;
}
#calForm textarea {
  height: 100px;
  resize: none;
}
#calForm input[type=button], #calForm input[type=submit] {
  border: 0; padding: 10px; margin: 10px 2px 0 0;
  color: #fff; background: var(--bg-color-c);
  cursor: pointer;
}

/* (X) DOES NOT MATTER */
#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 cal = {
  // (A) PROPERTIES
  // (A1) FLAGS & DATA
  sMon : false, // week start on monday
  data : null, // events for selected period
  sDay : 0, sMth : 0, sYear : 0, // selected day month year

  // (A2) MONTHS & DAY NAMES
  months : [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  ],
  days : ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"],

  // (A3) HTML ELEMENTS
  hMth : null, hYear : null, // month/year selector
  hWrap : null, // calendar wrapper
  hFormWrap : null, hForm : null, // event form
  hfDate : null, hfTxt : null, hfDel : null, // form fields

  // (B) INIT CALENDAR
  init : () => {
    // (B1) GET HTML ELEMENTS
    cal.hMth = document.getElementById("calMonth");
    cal.hYear = document.getElementById("calYear");
    cal.hWrap = document.getElementById("calWrap");
    cal.hFormWrap = document.getElementById("calForm");
    cal.hForm = cal.hFormWrap.querySelector("form");
    cal.hfDate = document.getElementById("evtDate");
    cal.hfTxt = document.getElementById("evtTxt");
    cal.hfDel = document.getElementById("evtDel");

    // (B2) APPEND MONTHS/YEAR
    let now = new Date(), nowMth = now.getMonth();
    cal.hYear.value = parseInt(now.getFullYear());
    for (let i=0; i<12; i++) {
      let opt = document.createElement("option");
      opt.value = i;
      opt.innerHTML = cal.months[i];
      if (i==nowMth) { opt.selected = true; }
      cal.hMth.appendChild(opt);
    }

    // (B3) ATTACH CONTROLS
    cal.hMth.onchange = cal.draw;
    cal.hYear.onchange = cal.draw;
    cal.hForm.onsubmit = cal.save;
    document.getElementById("evtClose").onclick = () => cal.hFormWrap.close();
    cal.hfDel.onclick = cal.del;

    // (B4) START - DRAW CALENDAR
    if (cal.sMon) { cal.days.push(cal.days.shift()); }
    cal.draw();
  },

  // (C) DRAW CALENDAR FOR SELECTED MONTH
  draw : () => {
    // (C1) DAYS IN MONTH + START/END DAYS
    // note - jan is 0 & dec is 11
    // note - sun is 0 & sat is 6
    cal.sMth = parseInt(cal.hMth.value); // selected month
    cal.sYear = parseInt(cal.hYear.value); // selected year
    let daysInMth = new Date(cal.sYear, cal.sMth+1, 0).getDate(), // number of days in selected month
        startDay = new Date(cal.sYear, cal.sMth, 1).getDay(), // first day of the month
        endDay = new Date(cal.sYear, cal.sMth, daysInMth).getDay(), // last day of the month
        now = new Date(), // current date
        nowMth = now.getMonth(), // current month
        nowYear = parseInt(now.getFullYear()), // current year
        nowDay = cal.sMth==nowMth && cal.sYear==nowYear ? now.getDate() : null ;

    // (C2) LOAD DATA FROM LOCALSTORAGE
    cal.data = localStorage.getItem("cal-" + cal.sMth + "-" + cal.sYear);
    if (cal.data==null) {
      localStorage.setItem("cal-" + cal.sMth + "-" + cal.sYear, "{}");
      cal.data = {};
    } else { cal.data = JSON.parse(cal.data); }

    // (C3) DRAWING CALCULATIONS
    // (C3-1) BLANK SQUARES BEFORE START OF MONTH
    let squares = [];
    if (cal.sMon && startDay != 1) {
      let blanks = startDay==0 ? 7 : startDay ;
      for (let i=1; i<blanks; i++) { squares.push("b"); }
    }
    if (!cal.sMon && startDay != 0) {
      for (let i=0; i<startDay; i++) { squares.push("b"); }
    }

    // (C3-2) DAYS OF THE MONTH
    for (let i=1; i<=daysInMth; i++) { squares.push(i); }

    // (C3-3) BLANK SQUARES AFTER END OF MONTH
    if (cal.sMon && endDay != 0) {
      let blanks = endDay==6 ? 1 : 7-endDay;
      for (let i=0; i<blanks; i++) { squares.push("b"); }
    }
    if (!cal.sMon && endDay != 6) {
      let blanks = endDay==0 ? 6 : 6-endDay;
      for (let i=0; i<blanks; i++) { squares.push("b"); }
    }

    // (C4) "RESET" CALENDAR
    cal.hWrap.innerHTML = `<div class="calHead"></div>
    <div class="calBody">
      <div class="calRow"></div>
    </div>`;

    // (C5) CALENDAR HEADER - DAY NAMES
    wrap = cal.hWrap.querySelector(".calHead");
    for (let d of cal.days) {
      let cell = document.createElement("div");
      cell.className = "calCell";
      cell.innerHTML = d;
      wrap.appendChild(cell);
    }

    // (C6) CALENDAR BODY - INDIVIDUAL DAYS & EVENTS
    wrap = cal.hWrap.querySelector(".calBody");
    row = cal.hWrap.querySelector(".calRow");
    for (let i=0; i<squares.length; i++) {
      // (C6-1) GENERATE CELL
      let cell = document.createElement("div");
      cell.className = "calCell";
      if (nowDay==squares[i]) { cell.classList.add("calToday"); }
      if (squares[i]=="b") { cell.classList.add("calBlank"); }
      else {
        cell.innerHTML = `<div class="cellDate">${squares[i]}</div>`;
        if (cal.data[squares[i]]) {
          cell.innerHTML += "<div class='evt'>" + cal.data[squares[i]] + "</div>";
        }
        cell.onclick = () => { cal.show(cell); };
      }
      row.appendChild(cell);

      // (C6-2) NEXT ROW
      if (i!=(squares.length-1) && i!=0 && (i+1)%7==0) {
        row = document.createElement("div");
        row.className = "calRow";
        wrap.appendChild(row);
      }
    }
  },

  // (D) SHOW EDIT EVENT DOCKET FOR SELECTED DAY
  show : cell => {
    cal.hForm.reset();
    cal.sDay = cell.querySelector(".cellDate").innerHTML;
    cal.hfDate.value = `${cal.sDay} ${cal.months[cal.sMth]} ${cal.sYear}`;
    if (cal.data[cal.sDay] !== undefined) {
      cal.hfTxt.value = cal.data[cal.sDay];
      cal.hfDel.classList.remove("hide");
    } else { cal.hfDel.classList.add("hide"); }
    cal.hFormWrap.show();
  },

  // (E) SAVE EVENT
  save : () => {
    cal.data[cal.sDay] = cal.hfTxt.value;
    localStorage.setItem(`cal-${cal.sMth}-${cal.sYear}`, JSON.stringify(cal.data));
    cal.draw();
    cal.hFormWrap.close();
    return false;
  },

  // (F) DELETE EVENT FOR SELECTED DATE
  del : () => { if (confirm("Delete event?")) {
    delete cal.data[cal.sDay];
    localStorage.setItem(`cal-${cal.sMth}-${cal.sYear}`, JSON.stringify(cal.data));
    cal.draw();
    cal.hFormWrap.close();
  }}
};
window.onload = cal.init;
              
            
!
999px

Console