Here you’ll Find Code Check All Checkbox With Javascript. Earlier I have shared many blogs on Check All Checkbox. and now I’m going to create a  Check All Checkbox With Javascript.


Check All Checkbox With Javascript With Source Code


HTML CODE  : 


<!DOCTYPE html>

<html lang="en">

    <head>

        <title>Check All Checkbox With Javascript</title>

        <!--Google Font-->

        <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap" rel="stylesheet">

        <!--Stylesheet-->

        <link rel="stylesheet" href="style.css">

    </head>

    <body>

        <div class="container">

            <input type="checkbox" id="option-all" onchange="checkAll(this)">

            <label for="option-all">Select All</label>

            <br>

            <input type="checkbox" id="option-a">

            <label for="option-a">Option A</label>

            <br>

            <input type="checkbox" id="option-b">

            <label for="option-b">Option B</label>

            <br>

            <input type="checkbox" id="option-c">

            <label for="option-c">Option C</label>

            <br>

        </div>

        <!--Script-->

        <script src="script.js"></script>

    </body>

</html>


CSS CODE : 


*,

*:before,

*:after{

padding: 0;

margin: 0;

box-sizing: border-box;

}

body{

height: 100vh;

background-image: linear-gradient(to right top, #f4f416, #d0f300, #a7f100, #75ee00, #12eb1b);

display: grid;

place-items: center;

}

.container{

display: inline-block;

background-color: #ffffff;

padding: 50px 70px;

border-radius: 8px;

box-shadow: 0 20px 25px rgba(0,0,0,0.25);

}

input[type="checkbox"]:not(:first-child){

margin-left: 20px;

}

input[type="checkbox"]{

margin-bottom: 20px;

cursor: pointer;

}

label{

font-family: "Poppins",sans-serif;

font-weight: 500;

color: #1f194c;

letter-spacing: 0.5px;

cursor: pointer;

}


JAVASCRIPT CODE  : 


var checkboxes = document.querySelectorAll("input[type = 'checkbox']");

function checkAll(myCheckbox){

if(myCheckbox.checked == true){

checkboxes.forEach(function(checkbox){

checkbox.checked = true;

});

}

else{

checkboxes.forEach(function(checkbox){

checkbox.checked = false;

});

}

}