Reputation: 19
I have dynamic table with some rows. I try to get the ".tags"-td-content and write it to the ".tagsNew"-td in each row.
<tr>
<td class="td1">
Date 1
</td>
<td class="td2">
Text 1
<div class="tags"><a href="http://home1.com">Row_1_Tag</a></div>
</td>
<td class="tagsNew">
</td>
</tr>
<tr>
<td class="td1">
Date 2
</td>
<td class="td2">
Text 2
<div class="tags"><a href="http://home2.com">Row_2_Tag</a></div>
</td>
<td class="tagsNew">
</td>
</tr>
<tr>
<td class="td1">
Date 3
</td>
<td class="td2">
Text 3
<div class="tags"><a href="http://home3.com">Row_3_Tag</a></div>
</td>
<td class="tagsNew">
</td>
</tr>
...
Desired result:
<tr>
<td class="td1">
Date 1
</td>
<td class="td2">
Text 2
<div class="tags"><a href="http://home1.com">Row_1_Tag</a></div>
</td>
<td class="tagsNew">
Row_1_Tag /* needed */
</td>
</tr>
<tr>
<td class="td1">
Date 2
</td>
<td class="td2">
Text 2
<div class="tags"><a href="http://home2.com">Row_2_Tag</a></div>
</td>
<td class="tagsNew">
Row_2_Tag /* needed */
</td>
</tr>
...
This is my script:
<script>
function getTag(){
row = $("tr");
tagNeeded = $('tr .tags').text()
$.each(row, function() {
$('.tagsNew').text(tagNeeded);
});
}
</script>
The code works, but I need only the the .tags-content from the specific row. The function pastes the content from all rows into the .tagsNew table-cell
This is happens with above code
<!-- Result with function getTag() -->
<tr>
<td class="td1">
Date 1
</td>
<td class="td2">
Text 1
<div class="tags"><a href="http://home1.com">Row_1_Tag</a></div>
</td>
<td class="tagsNew">
Row_1_Tag Row_2_Tag Row_3_Tag
</td>
</tr>
any help would be appreciated
Upvotes: 0
Views: 93
Reputation: 4474
Try this:
$('.tags').each(function() {
$this = $(this);
$this.closest('td').next().text($this.text());
});
Note that this is strictly related to your current exact structure: it's not intended for a general use.
Upvotes: 1
Reputation: 74420
You can use this to set relevant specific text:
$('.tagsNew').text(function(){
return $(this).closest('tr').find('.tags').text();
});
Upvotes: 2