Subscribe Us

LightBlog
Showing posts with label checkbox. Show all posts
Showing posts with label checkbox. Show all posts

Friday, 29 May 2020

May 29, 2020

How to get multiple checkbox value with all checkbox selection in jquery



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #count-checked-books {
            color:red;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <form action="#" id="bookfromsample">
        <p><label><input type="checkbox" id="checkAll"/>Book Name</label></p>
             <div id="booksection">
                 <p><label><input type="checkbox" class="booknamecls" name="bookname"  
                 value="book1"/> Book 1</label></p>
                 <p><label><input type="checkbox" class="booknamecls" name="bookname"  
                  value="book2"/> Book 2</label></p>
                 <p><label><input type="checkbox" class="booknamecls" name="bookname"  
                  value="book3"/> Book 3</label></p>
                 <p><label><input type="checkbox" class="booknamecls" name="bookname"  
                  value="book4"/> Book 4</label></p>
            </div>
            <span id="count-checked-books">0</span> selected books <br><br>
            <button type="button">get book</button><br><br>
             <div id="display"></div>
            <div id="display2"></div>
    </form>
</body>
<script>
$(document).ready(function () {
    //prop all checkbox selected or unseleted
    $('#checkAll').click(function () {
    debugger;
        $('.booknamecls').prop('checked', this.checked);
    });

    $('.booknamecls').change(function () {
    debugger;
        var check = ($('.booknamecls').filter(":checked").length == $('.booknamecls').length);
        $('#checkAll').prop("checked", check);
    });

    //checkbox value count
    var checkboxes = $('#bookfromsample p input:checkbox');
    checkboxes.change(function(){
    debugger;
        var countcheckbox = checkboxes.filter('#booksection :checked').length;
        $('#count-checked-books').text(countcheckbox);
    });

    //get checkbox value name parameters
    $('#bookfromsample input:checkbox').change(function(){
      debugger;
  var booknameval=$('#booksection input:checkbox').map(function(n){
          if(this.checked){
                return  this.value;
              };
       }).get().join(',');
       $('#display').html("formate one: "+booknameval);
    })

    // or other formate

    <!-- $("button").click(function(){ -->
    <!-- debugger; -->
        <!-- var bookname = []; -->
        <!-- $.each($("input[name='bookname']:checked"), function(){ -->
            <!-- bookname.push($(this).val()); -->
        <!-- }); -->
        <!-- $('#display2').html("formate two: "+bookname.join(",")); -->
    <!-- }); -->
});
</script>
</html>