All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get All Checked Checkbox Value In JavaScript

Last Updated : Mar 11, 2024

How To Get All Checked Checkbox Value In JavaScript

In this article we will show you the solution of how to get all checked checkbox value in JavaScript, in HTML, the checkbox is used to select one or multiple options from any page.

Now to get all checked checkbox values in JavaScript we have used the .checked property with the for loop and .length property.

.checked property: this property of JavaScript is used to set or reset the checkbox in the HTML page. By setting it true we can select checkboxes.

We can also deselect all the checkboxes by setting the .checked property to false.

Step By Step Guide On How To Get All Checked Checkbox Value In JavaScript :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> how to get all checked checkbox value in javascript </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
    <h1> TALKERSCODE </h1>
    <h3> how to get all checked checkbox value in javascript </h3>
    </center>
    <br>
    <input type="checkbox" name="carcheck" value="Hyundai"> Hyundai <br>
    <input type="checkbox" name="carcheck" value="Toyota"> Toyota <br>
    <input type="checkbox" name="carcheck" value="Kia"> Kia <br>
    <input type="checkbox" name="carcheck" value="Honda"> Honda <br>
    <input type="checkbox" name="carcheck" value="Renault"> Renault <br>
    <input type="checkbox" name="carcheck" value="Volkswagen"> Volkswagen <br>
    <input type="button" onclick="selectall()" value="select all">
    <input type="button" onclick="deselectall()" value="deselect all">
    <script type="text/javascript">
        function selectall() {
            let carcheck = document.getElementsByName("carcheck") ;
            let carchecklength = carcheck.length ;
            for(var x=0; x<carchecklength; x++) {
                carcheck[x].checked = true ;
            }
        }
        function deselectall() {
            let carcheck = document.getElementsByName("carcheck") ;
            let carchecklength = carcheck.length ;
            for(var x=0; x<carchecklength; x++) {
                carcheck[x].checked = false ;
            }
        }
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Creating a <style> tag to add CSS to the page. Also adding an external CSS file to Style the form
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here.
  8. Creating some checkbox input using <input> tag with the type of ‘checkbox’. Add the name of the checkbox at all <input> tag and values of different values.
  9. Using <br> to all the <input> checkbox to break into lines
  10. Now creating two button using <input> of type button with the ‘onclick’ of functions selectall() and deselectall() and the values of select all and deselect all
  11. Creating a <script> tag to write JavaScript within it
  12. At first create a function for selectall(). Within create a variable for carcheck with document.getElementByName() of ‘carcheck’.
  13. Using .length property of javascript to check the length of the carcheck variable.
  14. Now create a for loop and set the .checked property to true as per the length of the carcheck
  15. Then create a function of deselectall(). Within create a variable for carcheck with document.getElementByName() of ‘carcheck’.
  16. Again using .length property of javascript to check the length of the carcheck variable.
  17. Now create a for loop and set the .checked property to false as per the length of the carcheck
  18. Closing the script tag using </script>.
  19. Now by clicking on the select all button we can select all the checkboxes and by clicking on deselect all we can deselect all the checkboxes.
  20. We close the HTML file with </html> tag

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to get all checked checkbox value in javascript.

I hope this article on how to get all checked checkbox value in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪