Tuesday, 22 August 2017

how to get multiple checkbox checked value in jQuery

how to get multiple checkbox checked value in jquery

Description:

In this example we explain that how to get multiple checkbox checked value using jQuery. Or how to get multiple checkbox value and output as comma separated list. Or get multiple checkbox values to comma separated string using jQuery. Or how to get multiple selected checkbox values using jQuery.
Code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        function GetSelectedGames() {
            var allVals = [];
            $('#dvGames :checked').each(function () {
                allVals.push($(this).val());
            });
            $('#txtSelectedGames').val(allVals)
        }
        $(function () {
            $('#dvGames input').click(GetSelectedGames);
            GetSelectedGames();
        });
    </script>
    <title>jQuery multiple checkbox values to comma separated string
    </title>
</head>
<body>
  <div id="dvGames">
  <h1>Select Your Favorite Games</h1>
        <input type="checkbox" name="Cricket" id="Cricket" value="1" />
        Cricket<br />
        <input type="checkbox" name="FootBall" id="FootBall" value="2" />
        FootBall<br />
        <input type="checkbox" name="Tennis" id="Tennis" value="3" />
        Tennis<br />
        <input type="checkbox" name=" BasketBall" id="BasketBall" value="4" />
        Basket Ball<br />
        <textarea id="txtSelectedGames"></textarea>
    </div>
</body>
</html>



0 comments:

Post a Comment