muhammad aditya
muhammad aditya

Reputation: 63

get id from value id

how do i get the value of this id, i send it from from the controller and send it into the table

datail

my html

<tbody class="no-border-x">
   <tr>
     <td id="id"></td>
     <td id="nm_kegiatan"></td>
     <td id="biaya_pemeliharaan"></td>
     <td id="tgl_pemeliharaan"></td>
     <td id="penambahan_nilai_aset"></td>
     <td><a onclick='deleteKegiatan(this.id)' class="pointer-jempol"><i class="mdi mdi-delete"></i></a></td>
   </tr>
 </tbody>

my script load form controller

function showModal(id) { 
$('input[name=_method]').val('PATCH');
$('#modal-form form')[0].reset();
$.ajax({
  url: "{{ url('pemeliharaan/kegiatan') }}/" + id, //menampilkan data dari controller edit
  type: "GET",
  dataType: "JSON",
  success: function (data) { 
    var d = data[0];
    $('#modal-form').modal('show');
    $('#id').html(d.id);
    $('#nm_kegiatan').html(d.nm_kegiatan);
    $('#biaya_pemeliharaan').html(d.biaya_pemeliharaan);
    $('#tgl_pemeliharaan').html(d.tgl_pemeliharaan);
    $('#penambahan_nilai_aset').html(d.penambahan_nilai_aset);

  },
  error: function () {
    alert("Data tidak ada");
  }

});
}

Upvotes: 0

Views: 247

Answers (1)

Eddie
Eddie

Reputation: 26854

this.id does not refer to the <td id='id'>. It refers to the id of the clicked element. And id should be unique, so you cant have <td id="id"></td> on every row of the table.

  • Add class delete-row on delete <a>
  • Use class instead of id on <td>s

$("body").on('click', '.delete-row', function(event) {
  var id = $(this).parents('tr').find('.id').text();
  console.log(id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody class="no-border-x">
    <tr>
      <td class="id">1</td>
      <td class="nm_kegiatan"></td>
      <td class="biaya_pemeliharaan"></td>
      <td class="tgl_pemeliharaan"></td>
      <td class="penambahan_nilai_aset"></td>
      <td><a class="pointer-jempol delete-row"><i class="mdi mdi-delete"></i> Delete</a></td>
    </tr>
    <tr>
      <td class="id">2</td>
      <td class="nm_kegiatan"></td>
      <td class="biaya_pemeliharaan"></td>
      <td class="tgl_pemeliharaan"></td>
      <td class="penambahan_nilai_aset"></td>
      <td><a class="pointer-jempol delete-row"><i class="mdi mdi-delete"></i> Delete</a></td>
    </tr>
    <tr>
      <td class="id">3</td>
      <td class="nm_kegiatan"></td>
      <td class="biaya_pemeliharaan"></td>
      <td class="tgl_pemeliharaan"></td>
      <td class="penambahan_nilai_aset"></td>
      <td><a class="pointer-jempol delete-row"><i class="mdi mdi-delete"></i> Delete</a></td>
    </tr>
  </tbody>
</table>

Upvotes: 3

Related Questions