0

i would like to remove a row from a table using jQuery. Here is the html:

<tr>
    <td><center><a id="remove" href="#"><span class="glyphicon glyphicon-remove"></span></a></center></td>
</tr>

my jquery script:

$('td a').on('click',function(e){
//delete code.
e.preventDefault();
$(this).parent().remove();

});

when i click on the link, nothinh happens. Anybody can help me?

7
  • What do you see if you console.log(this) inside that handler? Commented Aug 16, 2017 at 20:31
  • 1
    Is your code in a document.ready() call? Side note, <center> doesn't exist anymore. Commented Aug 16, 2017 at 20:31
  • Have you verified the click is registering? Also if you have more than one of the "remove" links, you should use class="remove" instead of id="remove" since there should only be one of each id on a page. Commented Aug 16, 2017 at 20:45
  • if i console.log(this) nothing happens in the console. Commented Aug 16, 2017 at 20:57
  • my code is in a $(document).ready(function(){ $('td a').on('click',function(e){ //delete code. e.preventDefault(); $(this).closest('tr').remove(); console.log(this); }); }); Commented Aug 16, 2017 at 20:58

1 Answer 1

1

You need to either wrap your code in a $(document).ready call or move it to the end of the page before the closing body element (</body>). If you're executing that code in the head of your document you're running it against elements that don't yet exist.

Also if you want to remove the parent row, use $(this).closest('tr') instead of $(this).parent() as that would select the non-standard <center> element.

$(document).ready(function() {
  $('td a').on('click', function(e) {
    //delete code.
    e.preventDefault();
    $(this).closest('tr').remove();
  });
});

jsFiddle example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.