Reputation: 3324
I am trying to loop through all the elements in a tow on a button click
my button:
`<a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye"></i></a>`
my onclick method:
$(this).closest('tr').find('td').each(function() {
var textval = $(this).text();
console.log('textval');
});
For some reason this isn't printing our anything at all, its not even going into the code in the each.
I tried printing console.log($(this).closest('tr').find('td').text())
but it just shows empty text.
Here is my table rows:
<table id="productsTable" class="table-saas table table-hover" data-pagination="true" data-page-size="10">
<thead>
<tr>
<th style="width: 15%; " data-field="name">
<div class="th-inner ">Name</div>
<div class="fht-cell"></div>
</th>
<th style="width: 15%; " data-field="store_url">
<div class="th-inner ">Store URL</div>
<div class="fht-cell"></div>
</th>
<th style="width: 15%; " data-field="status">
<div class="th-inner ">Status</div>
<div class="fht-cell"></div>
</th>
<th style="width: 15%; " data-field="created_at">
<div class="th-inner ">Created At</div>
<div class="fht-cell"></div>
</th>
<th style="width: 25%; " data-field="notes">
<div class="th-inner ">Notes</div>
<div class="fht-cell"></div>
</th>
<th style="text-align: center; vertical-align: middle; width: 15%; " data-field="operate">
<div class="th-inner "></div>
<div class="fht-cell"></div>
</th>
</tr>
</thead>
<tbody>
<tr data-index="0">
<td style="width: 15%; ">test 1221321</td>
<td style="width: 15%; ">-</td>
<td style="width: 15%; ">pending review</td>
<td style="width: 15%; ">about 3 hours</td>
<td style="width: 25%; ">-</td>
<td style="text-align: center; vertical-align: middle; width: 15%; "><a href="#" style="margin-right: 5px" onclick="approveProduct()" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a><a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye"></i></a><a href="#" onclick="deleteProduct()" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a></td>
</tr>
<tr data-index="1">
<td style="width: 15%; ">prepend this product</td>
<td style="width: 15%; ">-</td>
<td style="width: 15%; ">pending review</td>
<td style="width: 15%; ">about 3 hours</td>
<td style="width: 25%; ">-</td>
<td style="text-align: center; vertical-align: middle; width: 15%; "><a href="#" style="margin-right: 5px" onclick="approveProduct()" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a><a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye"></i></a><a href="#" onclick="deleteProduct()" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a></td>
</tr>
<tr data-index="2">
<td style="width: 15%; ">test product 2</td>
<td style="width: 15%; ">-</td>
<td style="width: 15%; ">pending review</td>
<td style="width: 15%; ">about 3 hours</td>
<td style="width: 25%; ">-</td>
<td style="text-align: center; vertical-align: middle; width: 15%; "><a href="#" style="margin-right: 5px" onclick="approveProduct()" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a><a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye"></i></a><a href="#" onclick="deleteProduct()" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a></td>
</tr>
<tr data-index="3">
<td style="width: 15%; ">Test product append</td>
<td style="width: 15%; ">-</td>
<td style="width: 15%; ">pending review</td>
<td style="width: 15%; ">about 3 hours</td>
<td style="width: 25%; ">-</td>
<td style="text-align: center; vertical-align: middle; width: 15%; "><a href="#" style="margin-right: 5px" onclick="approveProduct()" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a><a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye"></i></a><a href="#" onclick="deleteProduct()" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a></td>
</tr>
<tr data-index="4">
<td style="width: 15%; ">Pet Grooming Deshedding Brush Glove </td>
<td style="width: 15%; ">-</td>
<td style="width: 15%; ">pending review</td>
<td style="width: 15%; ">about 2 months</td>
<td style="width: 25%; ">This is a test note to see wtf the table does with it</td>
<td style="text-align: center; vertical-align: middle; width: 15%; "><a href="#" style="margin-right: 5px" onclick="approveProduct()" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a><a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye"></i></a><a href="#" onclick="deleteProduct()" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a></td>
</tr>
<tr data-index="5">
<td style="width: 15%; ">LED Dog Collar</td>
<td style="width: 15%; ">-</td>
<td style="width: 15%; ">pending review</td>
<td style="width: 15%; ">about 2 months</td>
<td style="width: 25%; ">This is a test note to see wtf the table does with it</td>
<td style="text-align: center; vertical-align: middle; width: 15%; "><a href="#" style="margin-right: 5px" onclick="approveProduct()" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a><a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye"></i></a><a href="#" onclick="deleteProduct()" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a></td>
</tr>
<tr data-index="6">
<td style="width: 15%; ">Pet Grooming Deshedding Brush Glove </td>
<td style="width: 15%; ">-</td>
<td style="width: 15%; ">pending review</td>
<td style="width: 15%; ">about 2 months</td>
<td style="width: 25%; ">This is a test note to see wtf the table does with it</td>
<td style="text-align: center; vertical-align: middle; width: 15%; "><a href="#" style="margin-right: 5px" onclick="approveProduct()" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a><a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye"></i></a><a href="#" onclick="deleteProduct()" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a></td>
</tr>
</tbody>
loading my data via an ajax call:
$.ajax({
url: "/product/fetch_admin_products",
type: "GET",
data: {},
success: function(resp) {
if (resp) {
if (resp.products) {
setupTable(resp.products)
}
}
},
error: function(resp) {
},
});
$('#productsTable').bootstrapTable({
data: data,
columns: [
{ width: "15%" },
{ width: "15%" },
{ width: "15%" },
{ width: "15%" },
{ width: "25%" },
{
field: 'operate',
title: '',
width: '15%',
align: 'center',
valign: 'middle',
clickToSelect: false,
formatter : function(value,row,index) {
//return '<input name="elementname" value="'+value+'"/>';
return '<a href="#" style="margin-right: 5px" onclick="approveProduct()" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a>' +
'<a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye"></i></a>' +
'<a href="#" onclick="deleteProduct()" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a>'
}
}
],
});
Any clue on what i'm doing wrong? Should i be taking a diffrent approach to this?
Upvotes: 1
Views: 281
Reputation: 370689
The problem is that, in your code, the calling context of viewProduct
is window
, not the td
, resulting in $(this).closest('tr')
failing:
function viewProduct () {
console.log(this.location.href);
}
<table>
<tbody>
<tr data-index="0">
<td style="width: 15%; ">test 1221321</td>
<td style="width: 15%; ">-</td>
<td style="width: 15%; ">pending review</td>
<td style="width: 15%; ">about 3 hours</td>
<td style="width: 25%; ">-</td>
<td style="text-align: center; vertical-align: middle; width: 15%; "><a href="#" style="margin-right: 5px" onclick="approveProduct()" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a><a href="#" style="margin-right: 5px" onclick="viewProduct()" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye">view</i></a>
<a
href="#" onclick="deleteProduct()" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a>
</td>
</tr>
</tbody>
</table>
Instead, attach the event handler properly with Javascript, and you can reference the desired td
and its containing tr
explicitly. For example:
const td = document.querySelector('td:last-child');
td.onclick = function() {
[...td.parentElement.children].forEach(td => console.log(td.textContent));
}
<table>
<tbody>
<tr data-index="0">
<td style="width: 15%; ">test 1221321</td>
<td style="width: 15%; ">-</td>
<td style="width: 15%; ">pending review</td>
<td style="width: 15%; ">about 3 hours</td>
<td style="width: 25%; ">-</td>
<td style="text-align: center; vertical-align: middle; width: 15%; "><a href="#" style="margin-right: 5px" class="btn btn-success btn-xs"><i style="font-size: 15px" class="fa fa-arrow-right"></i></a><a href="#" style="margin-right: 5px" class="btn btn-info btn-xs"><i style="font-size: 15px" class="fa fa-eye">view</i></a>
<a
href="#" class="btn btn-danger btn-xs"><i style="font-size: 15px" class="fa fa-trash-o"></i></a>
</td>
</tr>
</tbody>
</table>
Upvotes: 1