Get values from the columns of a table and save the value of each column in an input

1

I have a table with 2 columns and I would like, when I click on any row, assign the values of each column of this row clicked on a <input type="text" /> . I have a code that it assigns however if I click on the first column it assigns the value of the first column or the value of the second column. How do I sort this out?

$('#tableTime tr td').click(function() {
    var content = $(this).text();
    $("#descricaoAgenda").val(content);
});
    
asked by anonymous 17.12.2015 / 13:34

1 answer

3

In your case, use the parent to select the tr parent of the td that was clicked, after accessing tr use the find function to select the td's daughters and use the to select each column of the selected row.

$('#tableTime tr td').click(function() {
    var content = $(this).parent().find(':nth-child(1)').text();
    $("#descricaoAgenda").val(content);
});
  

Tip : You can use the click event directly on the line of the $('table tr').on('click', callback) table, so you do not need to use parent() .

See example changed by hint , without using parent

$('table tr').on('click', function() {
  var content1 = $(this).find(':nth-child(1)').text();
  var content2 = $(this).find(':nth-child(2)').text();
  $("#txtResultado1").val(content1);
  $("#txtResultado2").val(content2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><table><tr><td>Jill</td><td>Smith</td></tr><tr><td>Eve</td><td>Jackson</td></tr></table><inputtype="text" id="txtResultado1" />
<input type="text" id="txtResultado2" />
    
17.12.2015 / 13:40