error in limiting checkboxes in laravel PHP

so i’m using laravel php and i’m trying to limit checked checkbox to 2 from my table. it does limit it to 2, but it still does the code that’s supposed to only work when the checked checkbox is <= 2. can anyone tell me where i did it wrong?

here’s my table

@foreach($testimony as $key => $value)
<div class="testimony">
 <tr>
  <td class="table-content">
   <input class="single-checkbox pinTestimony" type="checkbox" onclick="idT('{{$value->id}}')">
  </td>
 </tr>
</div>
@endforeach

here’s my script to limit it to 2 checkboxes

function idT(id){
  $('#testimony-id').val(id);
}

function checklimit(checkgroup, limit){
  for(var i = 0; i < checkgroup.length; i++){
    checkgroup[i].addEventListener('click', function(){
      var checkedcount = 0;
      for(var i = 0; i < checkgroup.length; i++){
        checkedcount+=(checkgroup[i].checked)? 1:0;
        if(checkedcount > limit){
          alert('Maximum Pinned Testimony is 2');
          this.checked = false;
          return false;
        }else{
          if(this.checked == true){
            var id = $('#testimony-id').val();
            $.ajax({
              headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
              type: "POST",
              url: "{{ url('/pin-testimony') }}" + "/" + id,
              success: function(response) {
                Swal.fire({
                title: 'Success',
                text: 'Pin success',
                icon: 'success'
                });
              },
              error: function (e) {
                console.log('error');
                console.log(e);
              }
            });
          }
        }
      }
    });
  }
}

checklimit(document.getElementsByClassName('pinTestimony'), 2);

the url in the ajax is only to change a value in the database, so it’s like a ‘flag’ and it works just fine.
whenever there are already two checked checkboxes and i check another one, it will show the alert ‘Maximum Pinned Testimony is 2’ but then it still goes into the else condition and change the ‘flag’ in my database. can anyone help me? thank you.