How to Get Selected Checkbox Value in Checkboxlist using JQuery?

Jul 04, 2024 | jQuery HTML


Hello Dev,

Here's a simplified clarification on a way to get the values of decided on checkboxes the usage of jQuery:

If you have a set of checkboxes and also you want to retrieve the values of those which can be checked whilst a button is clicked, jQuery makes this truthful. You can iterate over the checkboxes, locate those which can be checked (enter[type=checkbox]:checked), after which accumulate their values into an array.

Read Also: How to Get Root Path in Laravel Application?
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Get Selected Checkbox Values in jQuery</title>
    </head>
    <body>
        <h2>Checkbox List</h2>
        <label><input type="checkbox" value="1"> Checkbox 1</label><br>
        <label><input type="checkbox" value="2"> Checkbox 2</label><br>
        <label><input type="checkbox" value="3"> Checkbox 3</label><br>
        <label><input type="checkbox" value="4"> Checkbox 4</label><br><br>
        <button id="btnGetSelected">Get Selected Checkbox Values</button>
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script>
        $(document).ready(function() {
            $('#btnGetSelected').click(function() {
                var selectedValues = []; // Array to store selected checkbox values
                
                $('input[type=checkbox]:checked').each(function() {
                  selectedValues.push($(this).val()); // Push each checked checkbox value into the array
                });
                
                // Displaying selected checkbox values
                if(selectedValues.length > 0) {
                  alert('Selected Checkbox Values: ' + selectedValues.join(', '));
                } else {
                  alert('No checkboxes are checked.');
                }
            });
        });
        </script>
    </body>
</html>
Read Also: How to Disable a Anchor Tag in Using HTML?

In this Example:

We have a collection of checkboxes labeled "Checkbox 1" through "Checkbox four".

When the "Get Selected Checkbox Values" button is clicked (#btnGetSelected), jQuery listens for the press event.

Inside the press handler feature, we initialize an empty array selectedValues to save the values of selected checkboxes.

Using $('input[type=checkbox]:checked').Every(...), jQuery selects all checked checkboxes and iterates over them.

For each checked checkbox, $(this).Val() retrieves its value, that's then driven into the selectedValues array.

Finally, we display an alert with the selected checkbox values joined by using a comma, or a message indicating that no checkboxes are checked if selectedValues is empty.

This method lets in you to dynamically retrieve and manage the values of decided on checkboxes the usage of jQuery.

Feel free to ask if you have any questions or need further clarification!



Tags :
#jQuery
#HTML
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"