Friday, December 27, 2019

Lab 24 Hold Shift and Check Checkboxes

In this lab, I followed the instruction from JavaScript30 and recreated the Hold Shift and Check Checkboxes activity. First off, I made the HTML, CSS, and JavaScript files. Secondly, the <html>, <head>, <body>, <meta>, and <title> tags were already inserted in the HTML file. Thirdly, I typed in <link rel="stylesheet" type="text/css" href="Lab_24.css"> inside the <head> tags to link the CSS file with my HTML file. Fourthly, I typed in <script src="Lab_24.js" type="text/javascript"></script> inside the <body> tags which linked my JavaScript file with my HTML file. Inside the <body> tags, I typed the heading for the lab which has my name, date, and lab number with <div id="heading"><h1>Lab 24</h1><h2>Wei Jian Zhen</h2><h3>12/24/2019</h3></div>. After that, I typed in <div> tags and <input> tags which created a checklist. Lastly, I made the copyright notice with <p> and <div> tags. For the CSS I did html {width: 100%; height: auto;} body {background-color: #00FFA8; min-width: 100%; min-height: 100%;} to give the webpage a background color and make the webpage responsive to screen size changes. #heading {text-align: center; font-size: 16px;} to center the heading of the lab. #copyright {font-size: 16px; font-family: Baskerville, "Palatino Linotype", Palatino, "Century Schoolbook L", "Times New Roman", "serif"; font-weight: normal; padding-left: 1%; padding-right: 1%;} to change the font of the copyright notice. .inbox {max-width: 400px;
margin: 50px auto; background: white; border-radius: 5px; box-shadow: 10px 10px 0 rgba(0,0,0,0.1);} .item {display: flex; align-items: center; border-bottom: 1px solid #F1F1F1;} .item:last-child { border-bottom: 0;} input:checked + p { background: #F9F9F9; text-decoration: line-through;} input[type="checkbox"] {margin: 20px;} p {margin: 0; padding: 20px; transition: background 0.2s; flex: 1; font-family: 'helvetica neue'; font-size: 20px; font-weight: 200;border-left: 1px solid #D1E2FF;} That makes the structural look of the content of the webpage. For the JavaScript, I put:

// JavaScript Document

const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]');
let lastChecked;
function handleCheck(e) {
  // Check if they had the shift key down
  // AND check that they are checking it
  let inBetween = false;
  if (e.shiftKey && this.checked) {
    // go ahead and do what we please
    // loop over every single checkbox
    checkboxes.forEach(checkbox => {
      console.log(checkbox);
      if (checkbox === this || checkbox === lastChecked) {
        inBetween = !inBetween;
        console.log('Starting to check them in between!');
      }
      if (inBetween) {
        checkbox.checked = true;
      }
    });
  }
  lastChecked = this;
}
checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));

This makes the checkboxes clickable and interactable. Knowing how to make checklists is useful to a web designer when making forms and website with email accounts in them.

Link: http://techteach.us/Web2020/ZWeiJian/WCP/Labs/Lab_24/Lab_24.html


No comments:

Post a Comment