Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery closest TR selection

Hope someone can advise. Having issues trying to remove a row once a link has been clicked.

HTML

<table>
  <tr><td>Some Data</td><td><a href="#" class="remove-row>Remove Row</a></td></tr>
  <tr><td>Some Data</td><td><a href="#" class="remove-row">Remove Row</a></td></tr>
</table>

Now the JS

 $("a.remove-row").live('click', function(eve){
  eve.preventDefault();
  $.ajax({
   type: 'GET',
   url: '/someaction/',
   dataType: 'json',
   success: function(msg){
    if(msg.error){
     alert(msg.error);
    }else{
     $(this).closest('tr').remove();
     alert(msg.success);
    }    
   }
  })
 });

This should be real simple buts its not removing the row. Just for kicks if I change it to something like

$('.remove-row').addClass('foo');

It will add foo to all table rows. So can understand why its not removing the closest row.

Any Ideas ?

Thank in advanced.

like image 562
Lee Avatar asked Aug 23 '10 14:08

Lee


People also ask

Is closest JavaScript or jQuery?

The closest() is an inbuilt method in jQuery that returns the first ancestor of the selected element in the DOM tree. This method traverse upwards from the current element in the search of first ancestor of the element.

How do I find a specific parent in jQuery?

jQuery | parent() & parents() with ExamplesThe parent() is an inbuilt method in jQuery which is used to find the parent element related to the selected element. This parent() method in jQuery traverse a single level up the selected element and return that element.

How do you use nearest?

Definition and UsageThe closest() method searches up the DOM tree for elements which matches a specified CSS selector. The closest() method starts at the element itself, then the anchestors (parent, grandparent, ...) until a match is found. The closest() method returns null() if no match is found.


2 Answers

The problem is this currently refers to the ajax object in your success callback, but it's an easy fix, use the content option like this:

 $("a.remove-row").live('click', function(eve){
  eve.preventDefault();
  $.ajax({
   context: this,                    //add this here!
   type: 'GET',
   url: '/someaction/',
   dataType: 'json',
   success: function(msg){
    if(msg.error){
     alert(msg.error);
    }else{
     $(this).closest('tr').remove();
     alert(msg.success);
    }    
   }
  })
 });

The context option dictates what this will be in the $.ajax() callback functions, since you want it to be the .remove-row you clicked on, use this as the option.

like image 83
Nick Craver Avatar answered Oct 12 '22 00:10

Nick Craver


Nick's answer should work, but you can do this too, I don't know which one is better, probably Nick's one, but it may help anyway...

$("a.remove-row").live('click', function(eve){
  var row = this;
  eve.preventDefault();
  $.ajax({
   type: 'GET',
   url: '/someaction/',
   dataType: 'json',
   success: function(msg){
    if(msg.error){
     alert(msg.error);
    }else{
     $(row).closest('tr').remove();
     alert(msg.success);
    }    
   }
  })
 });
like image 21
NicolasT Avatar answered Oct 12 '22 01:10

NicolasT