LearnFast.Ninja

jQuery checkbox checked by name

In this tutorial you will see how to get an array of all the checked values from your checkboxs. We will get it by the name attribute which is the same for all our checkboxs. After that we will print the selected values in JavaScript alert.

Main idea:
$("input[name='MyGroup']:checked:enabled")

Working Example Code:

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    <script type="text/javascript">

        function get_Checked_Checkbox_By_Name(Input_Name) {

            var arr = [];

            $("input[name='" + Input_Name + "']:checked:enabled").each(function () {

                arr.push($(this).val());

            });

            $('#MyGroup_Hidden').val(arr);

            alert(arr);

        }

    </script>

</head>

<body>

    <input name="MyGroup" value="20" type="checkbox" />

    <input name="MyGroup" value="33" type="checkbox" />

    <input name="MyGroup" value="65" type="checkbox" />

    <input name="MyGroup" value="22" type="checkbox" />

    <input name="MyGroup" value="11" type="checkbox" />

    <br />

    <input id="MyGroup_Hidden" type="hidden" value="" runat="server" />

    <br />

    <div onclick="get_Checked_Checkbox_By_Name('MyGroup')">Click Here</div>

</body>

</html>


Result:


Tags

jquery

checkbox checked name