Reputation: 971
I have a table
<table id="favoriteFoodTable">
<th>
Food Name:
</th>
<th>
Restaurant Name:
</th>
<th>
</th>
<?php while ($row = $foods->fetch()) {
?>
<tr>
<td>
<?php echo $row['foodName']; ?>
</td>
<td>
<?php echo $row['restaurantName']; ?>
</td>
<td>
<a class="deleteLink" href="" >delete</a>
</td>
</tr>
<?php } ?>
</table>
I use this jquery function so when a user click on delete, the background of the row will change and the row then will delete
$(document).ready(function() {
$("#favoriteFoodTable .deleteLink").on("click",function() {
var td = $(this).parent();
var tr = td.parent();
//change the background color to red before removing
tr.css("background-color","#FF3700");
tr.fadeOut(400, function(){
tr.remove();
});
});
});
just the background is changing but the row is not deleted, why? how to solve?
Upvotes: 24
Views: 95842
Reputation: 41
Try
var tr = $(this).closest('tr');
tr.detach();
Worked for me!
Upvotes: -1
Reputation: 417
If you want only 1 row in the table to be selected at a time
$("#data tr").click(function() {
var selected = $(this).hasClass("highlight");
$("#data tr").removeClass("highlight");
if(!selected)
$(this).addClass("highlight");
Upvotes: 0
Reputation: 382514
The row is deleted but as clicking makes you follow the link, it's immediately restored when the page is refreshed.
Add return false;
or event.preventDefault();
at the end of the callback to prevent the default behavior :
$(document).ready(function() {
$("#favoriteFoodTable .deleteLink").on("click",function() {
var tr = $(this).closest('tr');
tr.css("background-color","#FF3700");
tr.fadeOut(400, function(){
tr.remove();
});
return false;
});
});
Note that I used closest
for a more reliable code : if another element comes in between, the tr
will still be found.
Upvotes: 60
Reputation: 37672
Here is another option.
function DeleteRowOfProductTable(productID){
$('#YourTableId tr').each(function (i, row) {
var $row = $(row);
var productLabelId = $row.find('label[name*="Product_' + productID + '"]');
var $productLabelIdValue = $productLabelId.text();
if (parseInt(productID) == parseInt($productLabelIdValue))
{
$row.remove();
}
});
}
Upvotes: 2
Reputation: 1540
What you forgot to do is to set hash in your link. example:
<a class="deleteLink" href="" >delete</a>
should be
<a class="deleteLink" href="#" >delete</a>
or
return false;
at end of your
$(document).ready(function() {
$("#favoriteFoodTable .deleteLink").on("click",function() {
...
return false;
});
});
Upvotes: 3