Reputation: 181
<tr>
<td>abc</td>
<td>Education</td>
<td>abc customer</td>
<td>123</td>
<td>[email protected]</td>
<td>
<button class="edit_record">Edit</button>
</td>
</tr>
My JS
$(function () {
$('body').on('click', '.edit_record', function (e) {
$.each($(this).closest('tr'), function (i, obj) {
//get all td text except last one
});
});
});
I tried
if(i != 5){console.log($(this).find('td').text());}
but it doesn't work, the last Edit value is included.
Upvotes: 3
Views: 2438
Reputation: 15555
$(document).on('click', '.edit_record', function() {
var table =$(this).closest('tr').find('td:not(:last)');
table.each(function () {
console.log($(this).text());
});
});
You can do it this way.Select all td except the last one then iterate on the selected td and get their respective text
Upvotes: 0
Reputation: 171669
Another approach is to target the siblings of the button's parent ... no need to fuss with index this way
$('body').on('click', '.edit_record', function (e) {
$(this).parent().siblings().each(function(){
console.log( $(this).text() );
});
});
reference siblings() docs
Upvotes: 3
Reputation: 24638
You can get the parent, using .parent(), .parent('td') or .closest('td')
, of the button and the find the .siblings
, which will be all td
s but the one containing the button. And, you can use .each()
instead of jQuery.each()
.:
$(this).closest('td').siblings().each(function(i, td) {
//The last td is excluded here as only the siblings are iterated.
});
$(function() {
$(document).on('click', '.edit_record', function() {
$(this).closest('td').siblings().each(function(i, td) {
console.log( td );
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table><tbody>
<tr>
<td>abc</td>
<td>Education</td>
<td>abc customer</td>
<td>123</td>
<td>[email protected]</td>
<td>
<button class="edit_record">Edit</button>
</td>
</tr>
</tbody></table>
Upvotes: 1
Reputation: 3934
Try this
$.each($(this).closest('tr').find('td').not(':last'), function (i, obj) {
//get all td text except last one
console.log($(this).html());
});
Upvotes: 3
Reputation: 9060
Your code will never reach index 5 because, you loop over the tr
element. Not its td
element, as table rows only existed 1 element only, use below code :
$.each($(this).closest('tr').find('td'), function(i, obj) {
// i right now having value 0 - 5(this is your last index)
});
Updated DEMO
Upvotes: 2