Skip to content

How to fix undefined url using JQuery in Codeigniter

i have a problem using sweetalert2 in my codeigniter project, the error is when i click the button confirm to delete, the button can’t get all of the url, so the page showing 404 error page, how to fix this error? this is the javascript with jquery

    $('.remove').on('click', function (e) {
      e.preventDefault();
      const href =$(this).attr('href');
      console.log($href)
      // var getUrl = $(this).attr('href', $(e.relatedTarget).data('href'));
    
      // var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
      Swal.fire({
        title: 'Apakah Yakin Hapus Data Ini?',
        text: "Data yang terhapus tidak dapat dikembalikan!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Ya, Hapus!',
        cancelButtonText: 'Tidak!'
      }).then((result) => {
        if (result.isConfirmed) {
          document.location.href = href;
          
        }
      });
    });

and this is the button using datatable

<td align="center" width="130px">                             
<?php
echo anchor(site_url('pegawai/read/' . $row->user_id), '<button type="button" data-toggle="modal" data-target="#exampleModal"class="btn btn-sm btn-primary" data-tooltip="tooltip" data-placement="bottom" title="detail"><i class="fa fa-eye"></i></button>');
echo ' ';
echo anchor(site_url('pegawai/update/' . $row->user_id), '<button class="btn btn-sm btn-success btn-flat" data-tooltip="tooltip" data-placement="bottom" title="edit"><i class="fa fa-edit"></i></button>');
echo ' ';
echo anchor(site_url('pegawai/delete/' . $row->user_id), '<button class="btn btn-sm btn-danger btn-flat remove" id="deletedata" data-tooltip="tooltip" data-placement="bottom" title="hapus"><i class="fa fa-trash"></i></button>');
?>
</td>

Answer

Solution 1:

var href =$(this).parent().attr('href');