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

Last Updated : Mar 11, 2024

How To Get Multiple Checkbox Value In jQuery

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

The map() method translates all items in an object or in array to a new array of items, get() method loads data from the server by using GET HTTP request which returns XMLHttpRequest object and join() helps us for joins element with each other with delimiter comma symbol.

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

Here we defined html block with five checkbox element with common name attribute, different value of value attribute and a button ‘Click Here’ 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 map(), get(), join() methods to get multiple checkbox values.

<!DOCTYPE html>
<html>
    <head>
        <title>Values of Multiple Selected Checkbox</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 res=$("input[name='Flower']:checked").map(function(){
                        return this.value;
                    }).get().join(', ');
                    alert("Checked Flowers "+res);
                });
            });
        </script>
    </head>
    <body>
        <input type="checkbox" value="Tulips" name="Flower">Tulips
        <input type="checkbox" value="Lilly" name="Flower">Lilly
        <input type="checkbox" value="Rose" name="Flower">Rose
        <input type="checkbox" value="Jasmin" name="Flower">Jasmin
        <input type="checkbox" value="Lotus" name="Flower">Lotus
        <button id="sel">Click Here</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 variable ‘res’ and using map() method we collecting each input element which are have name ‘Flower’ value and in checked state and which is done by ‘$("input[name='Flower']:checked")’ this line.
  8. Then within function we returning collected value by ‘this’ and outside of map function we appending get() method which will gets returned values and which is appends with join method there we adding each other elements together with comma(,).
  9. At last we displaying result of ‘res’ with message ‘Checked Flowers’ on alert method which is throws alert message on webpage.
  10. In html block we defined five input elements checkbox ‘Tulips,Lilly,Rose,Jasmine,Lotus’ with different values and comman name value ‘Flower’ and a button ‘Click Here’ with id attribute ‘sel’.
  11. 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 five checkboxes with different flower values and button Click Here, user needs to clicks on it then alert message will appear with texts of selected checkbox values like ‘Checked Flowers Tulips, Lilly, Rose’.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪