|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Hold Shift to Check Multiple Checkboxes</title> |
| 6 | +</head> |
| 7 | +<body> |
| 8 | + <style> |
| 9 | + |
| 10 | + html { |
| 11 | + font-family: sans-serif; |
| 12 | + background:#ffc600; |
| 13 | + } |
| 14 | + |
| 15 | + .inbox { |
| 16 | + max-width:400px; |
| 17 | + margin:50px auto; |
| 18 | + background:white; |
| 19 | + border-radius:5px; |
| 20 | + box-shadow:10px 10px 0 rgba(0,0,0,0.1); |
| 21 | + } |
| 22 | + |
| 23 | + .item { |
| 24 | + display:flex; |
| 25 | + align-items:center; |
| 26 | + border-bottom: 1px solid #F1F1F1; |
| 27 | + } |
| 28 | + |
| 29 | + .item:last-child { |
| 30 | + border-bottom:0; |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + input:checked + p { |
| 35 | + background:#F9F9F9; |
| 36 | + text-decoration: line-through; |
| 37 | + } |
| 38 | + |
| 39 | + input[type="checkbox"] { |
| 40 | + margin:20px; |
| 41 | + } |
| 42 | + |
| 43 | + p { |
| 44 | + margin:0; |
| 45 | + padding:20px; |
| 46 | + transition:background 0.2s; |
| 47 | + flex:1; |
| 48 | + font-family:'helvetica neue'; |
| 49 | + font-size: 20px; |
| 50 | + font-weight: 200; |
| 51 | + border-left: 1px solid #D1E2FF; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + </style> |
| 56 | + <!-- |
| 57 | + The following is a common layout you would see in an email client. |
| 58 | +
|
| 59 | + When a user clicks a checkbox, holds Shift, and then clicks another checkbox a few rows down, all the checkboxes inbetween those two checkboxes should be checked. |
| 60 | +
|
| 61 | + --> |
| 62 | + <div class="inbox"> |
| 63 | + <div class="item"> |
| 64 | + <input type="checkbox"> |
| 65 | + <p>This is an inbox layout.</p> |
| 66 | + </div> |
| 67 | + <div class="item"> |
| 68 | + <input type="checkbox"> |
| 69 | + <p>Check one item</p> |
| 70 | + </div> |
| 71 | + <div class="item"> |
| 72 | + <input type="checkbox"> |
| 73 | + <p>Hold down your Shift key</p> |
| 74 | + </div> |
| 75 | + <div class="item"> |
| 76 | + <input type="checkbox"> |
| 77 | + <p>Check a lower item</p> |
| 78 | + </div> |
| 79 | + <div class="item"> |
| 80 | + <input type="checkbox"> |
| 81 | + <p>Everything inbetween should also be set to checked</p> |
| 82 | + </div> |
| 83 | + <div class="item"> |
| 84 | + <input type="checkbox"> |
| 85 | + <p>Try do it with out any libraries</p> |
| 86 | + </div> |
| 87 | + <div class="item"> |
| 88 | + <input type="checkbox"> |
| 89 | + <p>Just regular JavaScript</p> |
| 90 | + </div> |
| 91 | + <div class="item"> |
| 92 | + <input type="checkbox"> |
| 93 | + <p>Good Luck!</p> |
| 94 | + </div> |
| 95 | + <div class="item"> |
| 96 | + <input type="checkbox"> |
| 97 | + <p>Don't forget to tweet your result!</p> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + |
| 101 | +<script> |
| 102 | + const checkboxes = document.querySelectorAll("input[type='checkbox']"); |
| 103 | + let lastChecked; |
| 104 | + |
| 105 | + const handleCheck = event => { |
| 106 | + let inBetween = false; |
| 107 | + if(event.shiftKey && event.target.checked) { |
| 108 | + checkboxes.forEach(checkbox => { |
| 109 | + if (checkbox === event.target || checkbox === lastChecked) { |
| 110 | + inBetween = !inBetween; |
| 111 | + } |
| 112 | + |
| 113 | + if (inBetween) { |
| 114 | + checkbox.checked = true; |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + lastChecked = event.target; |
| 119 | + } |
| 120 | + |
| 121 | + checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck)) |
| 122 | +</script> |
| 123 | +</body> |
| 124 | +</html> |
0 commit comments