0
Good people, I need to get the ID of the clicked one. My HTML is as follows:
<table id="datatable">
<thead>
<tr>
<th>Nome</th>
<th>Data de Nascimento</th>
<th>Telefone</th>
<th>Email</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach($clients as $client)
<tr class="editLink" id="{{ $client->id }}">
<td>{{ $client->name }}</td>
<td>{{ $client->birthdate }}</td>
<td>{{ $client->phone }}</td>
<td>{{ $client->email }}</td>
<td class="delete delete_client"><i class="material-icons red-text delete-tupla">delete_forever</i></td>
</tr>
@endforeach
</tbody>
</table>
I am using the Laravel Blade to iterate the object that comes from the Controller, my question is the following, if the user clicks this in a <i>
, as I do to get the id that is in the tr of it, I’m trying to do so:
$(this).closest('tr').html()
The JS is like this:
$(".delete_client").click(function(e){
console.log($(this).closest('tr').html());
});
The problem is that it always takes the tr of the first iteration, if I have 10 lines, it always takes the first one, and I need to take the one that was clicked..
Any hint?
Thank you, hugs
You can show Javascript/jQuery that you have in addition to the line that is in the question?
– Sergio
I edited there, thanks for the tip
– Hermus
Use
$(this).closest('tr').attr("id")
.– Sam
The father of
<i>
is thetd
, and thetr
is the grandfather.– Sam
Also note that you clicked on
<td>
and not in the<i>
. Confirm that it makes a difference to what you’re trying to do– Isac
@Isac I also had repaired that, but it makes no difference in this case.
– Sam
In fact it was already working, had already tried the $(this). Closest('tr'), but as was looking for the . html(), obviously he sent me everything he had inside the TR, when in fact I needed to put the . attr('id'). Thank you to those who replied!
– Hermus
@Hermus Put yourself the answer and then mark it as right. Good luck!
– Sam
@DVD yes to the
id
it just makes no difference, but if you try to navigate to theparent()
ornext()
for example already will. It should be kept in mind what you want and be careful. I would personally change the click to the<i>
if this is the initial idea regarding the click– Isac
@Isac I already find it best in
td
, because so the whole area of thetd
is clickable.– Sam