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">
  <div id="sort"></div>
  <h1>SEAT RESERVATION</h1>
  
  <!-- (A) SEAT LAYOUT -->
  <div id="layout"></div>

  <!-- (B) LEGEND -->
  <div id="legend">
    <div class="seat"></div> <div class="txt">Available</div>
    <div class="seat taken"></div> <div class="txt">Taken</div>
    <div class="seat selected"></div> <div class="txt">Your Chosen Seats</div>
  </div>

  <!-- (C) SAVE SELECTION -->
  <button id="save" onclick="reserve.save()">Reserve Seats</button>
  
    <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/seat-reservation-javascript/"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) SEAT & "COLOR CODE" */
.seat {
  text-align: center;
  padding: 20px 10px;
  border-radius: 10px;
  background: #f1f1f1;
}
.taken { background: #df0000; color: #fff; }
.selected { background: #87ff96; }

/* (B) SEATS LAYOUT */
#layout {
  max-width: 400px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 10px;
  margin-bottom: 20px;
}

/* (C) LEGEND */
#legend {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: 50px auto ;
}
#legend .txt {
  display: flex;
  align-items: center;
}

/* (D) SAVE */
#save {
  font-size: 20px;
  margin-top: 20px;
  padding: 10px 20px;
  border: 0;
  color: #fff;
  background: #00479f;
}

/* (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-1574267432644-f410f8ec2474?crop=entropy&cs=srgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NTIyNDkwNzE&ixlib=rb-1.2.1&q=85);
  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 */
#sort {
  width: 100px; height:100px;
  background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 512 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M349.565 98.783C295.978 98.783 251.721 64 184.348 64c-24.955 0-47.309 4.384-68.045 12.013a55.947 55.947 0 0 0 3.586-23.562C118.117 24.015 94.806 1.206 66.338.048 34.345-1.254 8 24.296 8 56c0 19.026 9.497 35.825 24 45.945V488c0 13.255 10.745 24 24 24h16c13.255 0 24-10.745 24-24v-94.4c28.311-12.064 63.582-22.122 114.435-22.122 53.588 0 97.844 34.783 165.217 34.783 48.169 0 86.667-16.294 122.505-40.858C506.84 359.452 512 349.571 512 339.045v-243.1c0-23.393-24.269-38.87-45.485-29.016-34.338 15.948-76.454 31.854-116.95 31.854z" /></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 reserve = {
  // (A) INIT
  init : () => {
    // (A1) GET LAYOUT WRAPPER
    let layout = document.getElementById("layout");

    // (A2) GENERATE SEATS
    for (let i=65; i<=68; i++) { for (let j=1; j<=4; j++) {
      let seat = document.createElement("div");
      seat.innerHTML = String.fromCharCode(i) + j;
      seat.className = "seat";
      seat.onclick = () => { reserve.toggle(seat); };
      layout.appendChild(seat);
    }}

    // (A3) FOR DEMO ONLY - RANDOM TAKEN SEATS
    let all = document.querySelectorAll("#layout .seat"),
        len = all.length - 1, rnd = [];
    while (rnd.length != 3) {
      let r = Math.floor(Math.random() * len);
      if (!rnd.includes(r)) { rnd.push(r); }
    }
    for (let i of rnd) {
      all[i].classList.add("taken");
      all[i].onclick = "";
    }
  },

  // (B) CHOOSE THIS SEAT
  toggle : (seat) => {
    seat.classList.toggle("selected");
  },

  // (C) SAVE RESERVATION
  save : () => {
    // (C1) GET SELECTED SEATS
    let selected = document.querySelectorAll("#layout .selected");

    // (C2) ERROR!
    if (selected.length == 0) { alert("No seats selected."); }

    // (C3) SELECTED SEATS
    else {
      // (C3-1) GET SELECTED SEAT NUMBERS
      let seats = [];
      for (let s of selected) { seats.push(s.innerHTML); }

      // (C3-2) DO SOMETHING WITH IT...
      alert(JSON.stringify(seats));
    }
  }
};

window.addEventListener("DOMContentLoaded", reserve.init);

              
            
!
999px

Console