jQuery Getting Selected Checkbox Values ?

Getting Selected HTML Checkbox Values in jQuery

This jQuery is used to get the selected checkbox list in the web page and finds the selected checkbox values and returns a comma separated list of values.

        // gets the selected html rows checkbox's

        function getSelectedCheckbox() {
            var selectedCheckbox = [];
            $('input[type=checkbox]').each(function () {
                if ($(this).is(":checked")) {
                    selectedCheckbox.push($(this));
                }
            });
            return selectedCheckbox;
        }
         // gets the selected checkbox values with comma separated list.
        function getSelectedSubjects() {
            var selectedSubjects = [];
            var selectedCheckbox = getSelectedCheckbox();
            for (var i = 0; i < selectedCheckbox.length; i++) {
                selectedSubjects.push($(selectedCheckbox[i]).val());
            }
            alert(selectedSubjects.join(","));
        }

Example jQuery code to get the selected HTML checkbox values

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Delete Row Dynamically</title>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script>

        function getSelectedCheckbox() {
            var selectedCheckbox = [];
            $('input[type=checkbox]').each(function () {
                if ($(this).is(":checked")) {
                    selectedCheckbox.push($(this));
                }
            });
            return selectedCheckbox;
        }
        function getSelectedSubjects() {
            var selectedSubjects = [];
            var selectedCheckbox = getSelectedCheckbox();
            for (var i = 0; i < selectedCheckbox.length; i++) {
                selectedSubjects.push($(selectedCheckbox[i]).val());
            }
            alert(selectedSubjects.join(","));
        }
    </script>
</head>
<body>

    <table border="0">
        <thead>
            <tr>
                <th>Interested Subjects</th>

            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" id="chkRow1" value="Maths" />Maths</td>
            </tr>
            <tr>
                <td><input type="checkbox" id="chkRow2" value="Physics"/>Physics</td>
            </tr>
            <tr>
                <td><input type="checkbox" id="chkRow3" value="Chemistry"/>Chemistry</td>
            </tr>
            <tr>
                <td><input type="checkbox" id="chkRow4" value="Biology" />Biology</td>
            </tr>
            <tr>
                <td><input type="checkbox" id="chkRow5" value="Accounts"/>Accounts</td>
            </tr>
        </tbody>

    </table>
    <button id="btnSelectedChoices" onclick="getSelectedSubjects();">Selected Choices</button>
</body>
</html>
Output:

Before selecting checkbox's

Getting Selected HTML Checkbox values
Getting Selected HTML Checkbox values

Once selected choices button is clicked

Getting Selected HTML Checkbox values

This jQuery is used to get the selected HTML checkbox values and displays the pop up message with checkbox values as comma separated values.

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^