Trying to matching the checkbox with same button

I have this code:

$(document).on('click','.subject_add',function() {
      var $ele = $("input[type='checkbox']");
    var $btn = $(this);
    var $sameCheck = $btn.attr('id') && $ele.is(':checked') == $ele.attr('id').split('_')[2];
    alert($sameCheck);
    if(!$ele.is(':checked')) {
      Swal.fire('Oops','Please select the Add-on','error');
      return;
    }
    if($sameCheck) {
      Swal.fire('Oops','Please select the correct Add-on for the button','error');
      return;
    }
}

I am trying to make sure that every checkbox checked from the button which has the same ID. But possibly something is going wrong where it is not matching the same checkbox with same button.

my html:

<div align="left">
  <p>
    <input type="checkbox" class="checkboxid" name="subject_add" id="subject_add_3">
  <a href="javascript:;" class="btn btn-sm btn-info subject_add" id="3">Yes</a></p>

  <p>
    <input type="checkbox" class="checkboxid" name="subject_add" id="subject_add_4">
  <a href="javascript:;" class="btn btn-sm btn-info subject_add" id="4">Yes</a></p>

</div>

101 thoughts on “Trying to matching the checkbox with same button”

Leave a Comment