简体   繁体   中英

get item from table cell and append to first cell within same row

How would I grab text from a td inside a table and insert it to the first td in the same row? --using jQuery

<table border=1>
  <tbody>
    <tr>
      <td>APPEND TEXT1 HERE</td>
      <td></td>
      <td>GRAB TEXT1 HERE</td>
    </tr>
    <tr>
      <td>APPEND TEXT2 HERE</td>
      <td></td>
      <td>GRAB TEXT2 HERE</td>
    </tr>
    <tr>
      <td>APPEND TEXT3 HERE</td>
      <td></td>
      <td>GRAB TEXT3 HERE</td>
    </tr>
  </tbody>
</table>
$('table td:first-child').text(function(){
    return $(this).siblings().last().text()
})

http://jsfiddle.net/VdjN8/

In case that you want to append the text( instead of replacing ):

$('table td:first-child').text(function(i, c){
    return c + ' ' + $(this).siblings().last().text()
})

Iterate over each row and append the text from the td at the desired offset to the first one:

$('tr').each(function(){
    $(this).find('td').eq(0).append($(this).find('td').eq(2).text());
});

Here is a demonstration: http://jsfiddle.net/pchEG/

Loop over each <tr> and grab the .html() from the last <td> child element.

$('tr').each(function(idx){
  var appendToNode = $(this).find('td:first-child');
  // And append to the existing HTML.
  appendToNode.html(appendToNode.html() + $(this).find('td:last-child').html());
});

http://jsfiddle.net/Lvfwt/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM