
List 내의 item들을 shift키로 한번에 선택해보자!

DOM 요소중에서 inbox 내부의 item들을 전부 접근해야한다forEach메서드로 배열을 순회하면서 해당 item들의 addEventListener() 를 부여한다
// inbox 내의 input들만 선택
const checkboxes = document.querySelecotorAll('.inbox input[type="checkbox"]')
checkboxes.forEach((checkbox) => checkbox.addEventListener("click", handleClick)
lastChecked란 변수를 선언해, 마지막으로 체크된 체크박스 정보를 저장한다.inBetween이라는 변수를 할당하여 연속하는 체크박스의 범위 안에 있는지 여부를 저장한다.addEventListener()내의 콜백함수로 handleClick 함수를 만들어 연결하고,handleCheck 함수에 첫번째 파라미터로 event 객체를 넘긴다if문을 사용해서 event객체 (즉,여기선 아이템들)가 shiftKey를 누른상태이고, this.checked 여기서의 this도 해당 아이템들을 가르킨다, 즉 해당 아이템들이 checked인 상태(true)일때만을 만족하는 로직을 작성한다if문이 true일경우 로직은, 다시 전역변수로 설정해놓았던 checkboxes 노드리스트들을 forEach로 각각 순환하여 다시 조건 처리를 하게된다.inBetween 값을 false로 처리해 반전시킨다inBetwwen 값이 true일 경우, 해당 체크박스의checked 속성을 true로 설정한다//전역변수
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;
}
if (inBetween) {
checkbox.checked = true;
}
});
}
lastChecked = this;
}
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;
}
if (inBetween) {
checkbox.checked = true;
}
});
}
lastChecked = this;
}
checkboxes.forEach((checkbox) => checkbox.addEventListener("click", handleCheck));