(JQuery) Find the contents of td from the selected tr

I am very new to jQuery, so apologies for what might be a very simple question.

I have a table, and when I click on the row, I want the details of the cells to be filled into the form:

such a simple example

<table id="table">
 <tr><td class="field1">1 </td><td class="field2">2 </td></tr>
</table>
<input id="input" type="text" />

So jQuery:

$(document).ready(function() {
   $('#table tr').click(function() {
       $test = $(this).find('td').context.innerText) //problem here *
       $('#input').val( );
   })
  • returns the inner text tr (ie "1 2"

How can i do this...

Thanks in advance

Andy

Edit: ok in my fever, I see that I messed up what I wanted to type, here is js, I try:

$(document).ready(function() {
   $('#table tr').click(function() {
       $field1 = $(this).find('td.field1').context.innerText) //problem here *
       $('#input1').val($field1);
       $field2 = $(this).find('td.field2').context.innerText) //problem here *
       $('#input12').val($field2);
   })

Confusion Applications

+3
source share
3 answers

If you want the text of each cell to be captured as a line, separated by a space, to fill your input, you can do this:

$(document).ready(function() {
   $('#table tr').click(function() {
       var $test = $(this).find('td').map(function() {
           return $(this).text();
       }).get().join(" ");
       $('#input').val($test);
   });
});

EDIT text(), :

var $field1 = $(this).find('td.field1').text();
+6
$(document).ready(function() { 
   var $test="";
   $('#table tr>td').click(function() { 
       $test = $(this).text(); 
       $('#input').val($test);
   )};
});

alernative:

$(document).ready(function() { 
  var $test ="";
  $('.field1, #table').click(function() { 
      $test = $(this).text(); 
      $('#input').val($test); 
  )};
  $('.field2, #table').click(function() { 
    $test = $(this).text(); 
    $('#input').val($test); 
  )};
});
0

:

HTML:

<tr class="" id="tr_id" >
    <td class="l_id">7283630222</td>
</tr>
<tr class="" id="tr_id" >
    <td class="l_id">7276684022</td>
</tr>
<input tyxp="text" id="leadID"></input>

JQuery

$(document).ready(function(){
    $("tr#tr_id").click(function(){
        $("#hiddenDiv").show();
        $("#leadID").val($(this).find("td.l_id").text());
    });
});
0
source

Source: https://habr.com/ru/post/1735912/


All Articles