Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Select values of checkbox group with jquery

Suggested Videos
Part 9 - jQuery case insensitive attribute selector
Part 10 - jQuery input vs :input
Part 11 - jQuery checked selector



In this video we will discuss, how to select values of checked checkboxes that are in different groups using jQuery. Along the way, we will also discuss how to pass a variable to jquery selector.



This is continuation to Part 11, please watch Part 11 from jQuery tutorial before proceeding.

If you have just one group of checkboxes on your page, to get all the checked checkboxes you can use $('input[type="checkbox"]:checked'). 
Select values of checkbox group with jquery

However, if you have more than one checkbox group, then $('input[type="checkbox"] :checked') is going to select all checked checkboxes from both the checkbox groups.
jquery get selected checkbox group value

If you prefer to get checked checkboxes from a specific checkbox group, depending on which button you have clicked, you can use $('input[name="skills"]:checked') or  $('input[name="cities"]:checked'). This will ensure that the checked checkboxes from only the skills or cities checkbox group are selected.

<html>
<head>
    <title></title>
    <script src="jquery-1.11.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnGetSelectedSkills').click(function () {
                getSelectedCheckBoxes('skills');
            });

            $('#btnGetSelectedCities').click(function () {
                getSelectedCheckBoxes('cities');
            });

            var getSelectedCheckBoxes = function (groupName) {
                var result = $('input[name="' + groupName + '"]:checked');
                if (result.length > 0) {
                    var resultString = result.length + " checkboxe(s) checked<br/>";
                    result.each(function () {
                        resultString += $(this).val() + "<br/>";
                    });
                    $('#divResult').html(resultString);
                }
                else {
                    $('#divResult').html("No checkbox checked");
                }
            };
        });
    </script>
</head>
<body style="font-family:Arial">
    Skills :
    <input type="checkbox" name="skills" value="JavaScript" />JavaScript
    <input type="checkbox" name="skills" value="jQuery" />jQuery
    <input type="checkbox" name="skills" value="C#" />C#
    <input type="checkbox" name="skills" value="VB" />VB
    <br /><br />
    Preferred Cities :
    <input type="checkbox" name="cities" value="New York" />New York
    <input type="checkbox" name="cities" value="New Delhi" />New Delhi
    <input type="checkbox" name="cities" value="London" />London
    <br /><br />
    <input id="btnGetSelectedSkills" type="submit" value="Get Selected Skills" />
    <input id="btnGetSelectedCities" type="submit" value="Get Selected Cities" />
    <br /><br />
    <div id="divResult">
    </div>
</body>
</html>

At the moment to get the checked checkboxes values, we are using a button click event. You can also use the click event of the checkbox to do this.
jquery get checkbox group checked value

<html>
<head>
    <title></title>
    <script src="jquery-1.11.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('input[name="skills"]').click(function () {
                getSelectedCheckBoxes('skills');
            });

            $('input[name="cities"]').click(function () {
                getSelectedCheckBoxes('cities');
            });

            var getSelectedCheckBoxes = function (groupName) {
                var result = $('input[name="' + groupName + '"]:checked');
                if (result.length > 0) {
                    var resultString = result.length + " checkboxe(s) checked<br/>";
                    result.each(function () {
                        resultString += $(this).val() + "<br/>";
                    });
                    $('#div' + groupName).html(resultString);
                }
                else {
                    $('#div' + groupName).html("No checkbox checked");
                }
            };
        });
    </script>
</head>
<body style="font-family:Arial">
    Skills :
    <input type="checkbox" name="skills" value="JavaScript" />JavaScript
    <input type="checkbox" name="skills" value="jQuery" />jQuery
    <input type="checkbox" name="skills" value="C#" />C#
    <input type="checkbox" name="skills" value="VB" />VB
    <br /><br />
    Preferred Cities :
    <input type="checkbox" name="cities" value="New York" />New York
    <input type="checkbox" name="cities" value="New Delhi" />New Delhi
    <input type="checkbox" name="cities" value="London" />London
    <br /><br />
    Selected Skills:<br />
    <div id="divskills"></div>
    <br />
    Selected Cities:<br />
    <div id="divcities"></div>
</body>
</html>

jQuery tutorial for beginners

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.