Project 10: Press shift to check multiple boxes
Tags: javascript, webdev, beginners, html
Published: 2021-12-11 18:17:17

Welcome to my "Build 30 Js Projects in 30 Days" Series .This is day 10 and project 10. If you haven't read the other articles in this series please check them out first. I'll list them at the end of this article.

As mentioned in my previous article. This is the Day 10 challenge of [Wes Bos Javascript30 course](https://javascript30.com/).

##<u>*Final result:*</u>
In this project we will focus on how to select multiple checkboxes by using shift.This is a very common features in website now a days. You can further add features to this and create a to-do-list project. 
{% codepen https://codepen.io/prachigarg19/pen/jOGMggK %}

[My source code](https://github.com/prachigarg19/Javacript_beginner_projects/tree/main/js30)

As always before starting download the starter files from [here](https://github.com/wesbos/JavaScript30). I've made a separate article on how to download starter files, you can check it out [here](https://dev.to/prachigarg19/build-30-javascript-projects-in-30-daysday-0-2m8j).

## PART 1:HTML
I've used a little bootstrap here. All you have to do is go to [Bootstrap input groups](https://getbootstrap.com/docs/5.1/forms/input-group/) and copy paste the code for checkbox along with input field format or you could copy it from my codepen. I wanted to focus on the js part for this challenge.
As you can see I've created input group and the thing to notice here is that I've given same id and class to radio and text box respectively.You will see it's use later.

## PART 2:CSS
Now we are going to style our project.
Here we have just made checkbox and text input box inline. We have also centered the container horizontally and vertically.

## PART 3:JAVASCRIPT
The idea is to check all the items between last checked item and the one clicked after pressing shift.
Now we will simply check for click event on radioboxes and call linethrough function for each element. 

linethrough function will simply set the style of the id element to line-through. 

```
function linethrough(id){
    value=id;
    document.querySelector(`.${value}`).style="text-decoration:line-through";
    console.log(value)
}

```
We will store id of checked element only if flag is false. This is used to make sure shift hasn't been pressed by user before checking radiobox.

```
for(radio of radioboxes){
    
    radio.addEventListener('click',(e)=>{
        id=e.target.id;
        linethrough(id);
        if(flag==false) last=e.target.id;
        //flag=false means shift is not pressed hence value of last is updated.
    })
    
```
If user presses shift then we will handle it separately.

Let' handle the shift case now. The idea is that we will store the id of the radio checked by user and then call linethrough function for each item. 

```
document.addEventListener('keydown',(e)=>{
    if(e.key==='Shift')
    {   flag=true; //set flag to true
        Array.from(radioboxes).forEach(radio=>{
            //now we will store the index of radio to the current.
            radio.addEventListener('click',(e)=>{
                current=e.target.id;
                selectMultiple();
                
            })
        })
    }
})

function selectMultiple(){
    //this will take numeric value in index. 
    currindex=current[current.length-1];
     lastindex=last[last.length-1];
     //start will take the smaller value of both index numeric part. Loop will begin from here
    start=(currindex<lastindex?currindex:lastindex);
    //end takes the larger value
    end=(currindex>lastindex?currindex:lastindex);
    //loop over each element.Check and line through each element.
        for(i=start;i<=end;i++)
        { id=`group-${i}`;
          document.getElementById(`${id}`).checked=true;
           linethrough(id);
        }  
}
```
Since our index is of format group-1,group-2 etc so we will take the last char of these strings to use them to compare their values and to iterate through loop.
Since user can click above or below the previously checked element, so we will initiate the smaller value to start and larger to end ,to iterate through loop. 

##<u>Things I learnt:</u>
 I didn't learn anything new as such, but this one was a bit tricky as compared to previous ones.

## <u>Previous article from this series:</u>
[Day 9 Project 9](https://dev.to/prachigarg19/project-9-must-know-dev-tools-tricks-1lb6), in this project I discussed about some dev tool tricks that one should know about. Do check it out if you haven't yet.

## <u>Conclusion</u>

That's it for today's project.Next project will be HTML5 Video Player.

If you have any doubts or suggestions please do let me know in the comment section. I'll be more than happy to interact with you.

If you like this series and want to be a part of it, do consider following me at @prachigarg19

Thanks for reading. :)