Ad Unit (Iklan) BIG

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

<!DOCTYPE html>
<html lang="en">
<head>
    <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>
            </div>
        <span id="count-checked-books">0</span> selected books
        <div id="display"></div>
    </form>
</body>
<script>
$(document).ready(function () {
    //prop all checkbox selected or unseleted
    $('#checkAll').click(function () {
        $('.booknamecls').prop('checked', this.checked);
    });

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

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

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

Post a Comment

0 Comments