All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Get Multiple Checkbox Value In jQuery Using Array

Last Updated : Mar 11, 2024

How To Get Multiple Checkbox Value In jQuery Using Array

In this article we will show you the solution of how to get multiple checkbox value in jQuery using array, here we are going to show you example for get multiple checkbox value with the help of each(), push() and join() methods.

The each() method specifies a function that runs for every matched element, push() method used for insert each elements into an array and join() helps us for join element with each other with delimeter comma symbol.

Step By Step Guide On How To Get Multiple Checkbox Value In jQuery Using Array :-

Here we defined html block with four checkbox element with common name attribute, different value of value attribute and a button ‘Click Me’ with id attribute.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that defined click() method with id ‘sel’ so which is refers when clicks on ‘Click Here’ click() event will load particular process.

There we defined each(), push(), join() methods to get multiple checkbox values.

<!DOCTYPE html>
<html>
    <head>
        <title>Values of Multiple Selected Checkbox Using Array</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("#sel").click(function(){
                    var arr=[];
                    $.each($("input:checkbox[name='colors']:checked"), function(){
                        arr.push($(this).val());
                    });
                    alert("Values "+arr.join(", "));
                })
            });
        </script>
    </head>
    <body>
        <input type="checkbox" value="Green" name="colors">Green
        <input type="checkbox" value="Rose" name="colors">Rose
        <input type="checkbox" value="Yellow" name="colors">Yellow
        <input type="checkbox" value="Pink" name="colors">Pink
        <button id="sel">Click Me</button>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() and click() method within that we defined empty array variable arr and using each() method we collecting all checked checkbox values by specifying common name attribute with ‘colors’ value.
  8. Which is done by ‘input:checkbox[name='colors']:checked’ this line, it selects all checkbox type input element have name attribute value as ‘colors’ and in checked state.
  9. Then within function we adding push() method to array variable ‘arr’ those selected elements will inserted on array by this method and each iteration outside we adding each other elements with comma(,) symbol by join() method.
  10. At last we adding array result with message ‘Values’ on alert method which is throws alert message on webpage.
  11. In html block we defined four input element checkbox ‘Green,Rose,Yellow,Pink’ with different values and comman name value ‘colors’ and a button ‘Click Me’ with id attribute ‘sel’.
  12. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get multiple checkbox values using jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see four checkboxes with different values and button Click Me, user needs to clicks on it then alert message will appear with texts of selected checkbox values like ‘Values Green, Rose, Pink’.

I hope this article on how to get multiple checkbox value in jQuery using array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪