Take the ID of a record in a PHP table with JS

1

Cordial Greeting.

I need your collaboration.

I have a php table: with BD records

<td  id="<?php echo $row['Solc_idx']; ?>"><input type="button" class="btn btn-primary" value="Procesar"></td>

The case is that that would be the ID, of the record of the BD and I capture it with JS, in the following way:

 $(function(){

  $('table').on('click', 'td', function(){ 

      console.log( 'Valor: ' + $(this).prop('id') );
  });
});

The case is that way that way if I take the ID, but I take it when I click anywhere in the TD, I mean the row, what I want is to take it only when you click on the button that is in the row of the table, not in the whole table

I do not know if they understand me.

Thank you, I remain attentive

    
asked by Andres Rodriguez 09.11.2018 в 17:22
source

1 answer

1

For this you must indicate, in the delegation of events, that the click will be on the button, and not on the td on the line $('table').on('click', 'td', function() , as is your case.

Here you must indicate that the click will be on the button, using for example the class. Then, at the time of obtaining the value, you must indicate that the attribute you want to receive is the id of the parent element ( td is the parent element of the input , which is the one that is receiving the event).

I leave you a small example;

$("table").on("click",".btn", function() {
  console.log("ID del elemento td: ", $(this).parent().attr("id"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<td  id="1"><input type="button" class="btn btn-primary" value="Procesar"></td>
<td  id="2"><input type="button" class="btn btn-primary" value="Procesar"></td>
<td  id="3"><input type="button" class="btn btn-primary" value="Procesar"></td>
</table>
    
answered by 09.11.2018 / 17:34
source