Sunday, January 8, 2017

JS30 Challenge Day10 - Hold shift to select checkboxes (buggy code)


text-decoration:line-through;

const checkboxes=document.querySelectorAll(‘input [type=checkbox]’);
let lastChecked;
function checkIt(e){
let flag=false;
if(e.shiftKey && this.checked){
checkboxes.forEach(checkbox=>{
if(this===checkbox || checkbox===lastChecked){
flag=!flag;
}
if(flag)
checkbox.checked=true;
});
}
lastChecked=this;
}

checkboxes.forEach(checkbox=>checkbox.addEventListener(‘click’,checkIt));

BUG:
The logic in this code is not complete though.
For example:

-a
-b
-c
-d
-e

If you check a to c.
then you uncheck b.
Then you press shift and check b, 

all the checkboxes below c get checked.

No comments:

Post a Comment