GithubHelp home page GithubHelp logo

gerhynes / checkboxes Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 3 KB

A page for working on checkbox selection. Built for Wes Bos' JavaScript 30 course.

Home Page: https://gk-hynes.github.io/checkboxes/

CSS 21.37% JavaScript 23.32% HTML 55.31%
javascript javascript30 checkbox-selection

checkboxes's Introduction

A page for working on checkbox selection. Built for Wes Bos' JavaScript 30 course.

Screenshot of checkbox page

Notes

Select all the checkboxes, loop over them and listen for a click.

When a box is checked run the handleCheck function.

Create a variable, lastChecked, to log the first box that is checked so that when the second box is checked while shift is held down you can know what the last box was. At the end of the function set lastChecked to this so that it will refer to the most recently checked checkbox.

Check if the shift key is down and a box is being checked.

 if (e.shiftKey && this.checked) { run code }

Loop over every checkbox, look for the first one that was checked, start checking boxes, and stop checking when you hit the last box that was checked.

Don't try to figure out where in the DOM the checkboxes are, and which elements are inbetween. This is quite fragile.

Instead, create a variable inBetween and set it to false. When the loop reaches the first checked checkbox, set inBetween to true and start checking checkboxes. When the loop reaches the last checkbox, set inBetween to false.

If inBetween is true set checkbox.checked to true.

checkboxes.forEach(checkbox => {
  if (checkbox === this || checkbox === lastChecked) {
    inBetween = !inBetween;
  }
  if (inBetween) {
    checkbox.checked = true;
  }
});

By checking if the loop is hitting this or lastChecked, the function works from both top to bottom and bottom to top.

By using inBetween = !inBetween you can swap it from true to false or false to true.

checkboxes's People

Contributors

gerhynes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.