0

I am facing a little problem with multiple delete in Laravel. Whenever I did not check a checkbox I want the button to be disabled and vice-versa.

$(document).ready(function() {
  //$("#btn_del").click(function() {
  //  $("#btn_del").prop("disabled", true);
  //})

  $('.ids').click(function() {
    var multiple_contact = [];    
      $('input:checkbox[name="multiple_contact[]"]:checked').each(function() {
      multiple_contact.push($(this).val());
    });

    var length = multiple_contact.length;
    if (length > 0) {
      $('#btn_del').attr('disabled', true);
      // $(".delete_contact").addClass();
    } else {
      // $(".delete_contact").removeClass();
      $('#btn_del').attr('disabled', false);
    }

    /*if ($(this).is(':checked')) {
      $('#btn_del').prop("disabled", false);
    } else {
      if ($('.checks').filter(':checked').length < 1) {
        $('#btn_del').attr('disabled',true);
      }
    } */
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="form-check-input  ids" name="multiple_contact[]" value="{{$admin_getcontact->id}}">
<button type="button" class="btn btn-success delete_contact" data-toggle="modal" data-target="#modalMultipleContactDelete" id="btn_del">Delete Multiple Contact Messages</button>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
mutairu
  • 67
  • 6
  • 16
  • 1
    Can you clarify your exact issue. The title is about deleting data whereas the description and code example is simply about toggling a button disabled/enabled using a checkbox. The latter of which already seems to be working. – Rory McCrossan Jan 06 '20 at 13:45
  • This looks like a html/javascript related problem rather than a php/laravel one – enbermudas Jan 06 '20 at 14:12
  • exactly what you said.is about toggling a button disabled/enabled using checkbox.But it has to do with multiple deletion u can see array in the checkbox attribute name – mutairu Jan 06 '20 at 14:18
  • the code is not working on my local machine – mutairu Jan 06 '20 at 14:56

1 Answers1

0

Ok, I copied this code into jsfiddle and I noticed that button was disabling but it was other way around means, it was disabling when I was clicking the check box, so I changed it the other way around and also I am disabling it by default on load.

Please find the jsfiddle for code https://jsfiddle.net/0dr69Lgt/

  $(document).ready(function() {

    $('#btn_del').attr('disabled', true);

  //$("#btn_del").click(function() {
  //  $("#btn_del").prop("disabled", true);
  //})

  $('.ids').click(function() {
    var multiple_contact = [];    
      $('input:checkbox[name="multiple_contact[]"]:checked').each(function() {
      multiple_contact.push($(this).val());
    });

    var length = multiple_contact.length;
    if (length > 0) {
      $('#btn_del').attr('disabled', false);
      // $(".delete_contact").addClass();
    } else {
      // $(".delete_contact").removeClass();
      $('#btn_del').attr('disabled', true);
    }

       console.log(multiple_contact);
    /*if ($(this).is(':checked')) {
      $('#btn_del').prop("disabled", false);
    } else {
      if ($('.checks').filter(':checked').length < 1) {
        $('#btn_del').attr('disabled',true);
      }
    } */
  });
});
  • thanks so very much @Gursharn Singh the problem i have now is that the thing is not functioning well.when document bload is going to disable but when i toggled it it would not work again.The checkbox is in inform of this pls help – mutairu Jan 08 '20 at 03:34
  • Sorry I didn't get your question, can you please explain whats not working? – Gursharn Singh Jan 08 '20 at 04:01
  • i said on document ready it will disable but when i clicked on the checboxes i mean when i toggled it it will not function in a way is supposed to.You know when u toggled is supposed to be disabled and able d, but in my case it is not – mutairu Jan 08 '20 at 12:08
  • Can you please add your associated HTML and Script into jsFiddle so that we can see more code. – Gursharn Singh Jan 08 '20 at 22:06
  • what i discovered is that the code is working on jsfiddle but not in sublime text i am using.You know actually it is php that actuall populated the checkbox if you see in the i posted one checkbox and its value as an array meaning that there are going to be many checkboxes .i think where the problem lt is now is the issue of the php being ppopulated the checkboxes may be that is the reason why jquery is not listening to the event .if i can solve this thing we the help of you and other guys i would be glad – mutairu Jan 10 '20 at 14:42