title: Let’s Build: With JavaScript – Dynamic Checkboxes published: true tags: javascript, tutorial, webdev, showdev canonical_url: https://web-crunch.com/javascript-dynamic-checkboxes/


Welcome to another installment of my Let’s Build: With JavaScript series. This video tutorial teaches you how to make a collection of dynamic HTML checkboxes using vanilla JavaScript.

The ongoing Let’s Build: With JavaScript series is a collection of tutorials that I have compiled to give those new to the language or those looking to finally learn the core components, a fresh take on solving common problems. At my day job, I’m faced with all types of complex problems and scenarios that require JavaScript to solve. Those problems ultimately inspire these tutorials and the future ones to come.

View the codepen

Getting Started

{% youtube znxoeUbBwi4 %}

In applications like Dropbox, Asana, Google Drive, and more there is commonly a list of files/tasks/etc. Each list item often allows you to perform certain actions either independently or in bulk. Within those patterns, I often see checkboxes that allow you to narrow in actions on a particular list item. Sometimes you may want to perform bulk actions on several actions. Doing this gets more complex but is very possible.

I’ll be solving a few of those patterns in this tutorial including:

  • Selecting all checkboxes at once
  • Shift + clicking to select multiple checkboxes
  • Clearing all checkbox selections at once
  • Adding an alternate state to those items that are checked

This all happens in less than 90 lines of JavaScript.

HTML

The HTML is basic markup with a few classes used to target the elements via JavaScript and CSS.

```html

Dynamic Checkboxes with JavaScript

Publish? Author Created at
1
Andy August 23, 2019
2
Randy August 22, 2019
3
John August 21, 2019
4
Jane August 20, 2019
5
Ryan August 19, 2019
6
Nicole August 18, 2019

Dynamic Checkboxes with JavaScript

Publish? Author Created at
1
Andy August 23, 2019
2
Randy August 22, 2019
3
John August 21, 2019
4
Jane August 20, 2019
5
Ryan August 19, 2019
6
Nicole August 18, 2019

```

CSS

The CSS is basic markup to make our tables look more presentable. I also add some custom styling for those items in a checked state.

```css @import url("https://fonts.googleapis.com/css?family=Montserrat:400,400i,700");

body { background: peachpuff; font-family: 'Montserrat'; }

.container { max-width: 700px; margin: 2rem auto; background: #fff; padding: 40px; border-radius: 10px; }

.table { width: 100%; margin-bottom: 1rem; color: #212529; border-collapse: collapse; }

.table thead th { border-bottom: 2px solid #dee2e6; border-top: 0; vertical-align: bottom; user-select: none; }

.table td, .table th { padding: 0.75rem; border-top: 1px solid #dee2e6; user-select: none; }

td.title { font-family: 'Georgia', serif; font-style: italic; }

button { background: #f4f4f4; padding: 10px 16px; margin-bottom: 10px; border-radius: 3px; appearance: none; border: 0; border-radius: 8px; line-height: normal;

&:hover { background: #f0f0f0; cursor: pointer; } }

.published { display: flex; align-items: center;

label { margin-left: 16px; font-family: "Georgia", serif; font-size: 16px; font-style: italic; } }

input[type=checkbox]:checked + label { text-decoration: line-through; }

```

Finally, the JavaScript

I use an object-oriented approach towards JavaScript. Everything lives within a global object I created that describes the feature. There are many other ways to write your JavaScript code so I don’t want you to think this is the only way. I found this way quite helpful for me in terms of reusability. I can share logic quite easily between functions and methods within the global object. This also is scoped to the global object which means fewer conflicts with JavaScript written elsewhere.

```javascript const DynamicCheckboxes = { checkboxes: document.querySelectorAll('.table td input[type="checkbox"]'), selectAllTarget: document.querySelector('.js-select-all'), clearBtn: document.querySelector('.js-clear'),

initialize() { this.shiftToSelect(); this.selectAll(); this.clearChecked(); this.showRemoveCheckedButton(); },

shiftToSelect() { const checkboxes = this.checkboxes; let lastChecked;

function handleCheck(event) {
  // Check if shift key is down and check if checkbox is checked
  let inBetween = false;
  if (event.shiftKey && this.checked) {
    checkboxes.forEach(checkbox => {
      if (checkbox === this || checkbox === lastChecked) {
        inBetween = !inBetween;
      }

      if (inBetween) {
        checkbox.checked = true;
      }
    });
  }
  lastChecked = this;
}

checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck, false));

},

selectAll() { function handleSelectAll(event) { this.checkboxes.forEach(checkbox => { checkbox.checked ? (checkbox.checked = false) : (checkbox.checked = true) }) }

this.selectAllTarget.addEventListener('click', handleSelectAll.bind(this), false)

},

showRemoveCheckedButton() { this.clearBtnDisplay('none') document.addEventListener('change', this.showBtn.bind(this)) },

showBtn(event) { const checkboxesChecked = document.querySelectorAll('.table td input[type=checkbox]:checked').length

if (checkboxesChecked > 0) {
  this.clearBtn.querySelector('span').textContent = checkboxesChecked;
  this.clearBtnDisplay('block');
} else {
  this.clearBtn.querySelector('span').textContent = '';
  this.clearBtnDisplay('none');
}

},

clearBtnDisplay(state) { this.clearBtn.style.display = state; },

clearChecked() { this.clearBtn.addEventListener('click', removeChecked.bind(this), false);

function removeChecked() {
  this.checkboxes.forEach(checkbox => (checkbox.checked = false));
  this.selectAllTarget.checked = false;
  this.clearBtnDisplay('none');
}

}

};

DynamicCheckboxes.initialize();

```

I recommend watching the video to see this come to life and hear my thinking as I’m putting it all together. At first glance, the code looks a bit complicated but in theory, each function within the DynamicCheckboxes object is a building block for the features we wanted to add in the first place. Some functions share logic while others act independently. You can probably guess, scaling something like this is quite hard. That’s likely why the rise of frameworks is all the buzz at the moment.

Closing things out

Hopefully, you’ve learned a bit here! I approached JavaScript in an ignorant manner. I wanted to build everything with it but then realized I needed to take a step back and focus on understanding smaller aspects of problems we encounter day-to-day as developers. There is always going to be better ways to write code but I found that first solving the problem allows you to take a new look at what you’ve accomplished and then later refactor into something more legible and performant. Sometimes writing less code actually can make things harder to read so doing beat yourself up if you look at code that’s been refactored to the most extreme amount.

The Series So Far

The post Let’s Build: With JavaScript – Dynamic Checkboxes appeared first on Web-Crunch.