0

I dynamically create an html table and one of the cells in each row stores a button that I want to delete that row when pressed. What are my options in knowing what table row to delete from the delete button pressed?

Am I somehow able to get the row that the button is in? Maybe then inside the click I could use the events 'this' property to get the button and then find out which cell it's in, and from there which row that cell is in? Not sure how I'd go about doing that though.

1
  • 2
    In jQuery you would just do $(this).closest('tr') Commented Nov 29, 2012 at 3:37

3 Answers 3

1

if you are using jQuery this will be a sample code on how you can achieve this.

$('#my_table_id').on('click', 'button', function() {
    $(this).closest('tr').remove();
});

hope this helps, best!

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

2 Comments

I like that solution. I like using jQuery. instead of the $. Do I just do jQuery(this).closest('tr').remove() then in your example?
You can, but I like using $
1

Put this in the button's onclick handler:

this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);

Comments

1

Can be done with native JS, but here is a jQuery solution. Be sure to clone(true) to ensure your events are preserved.

<tr><td>hello</td><td><span class="deleteMe">Delete</td></tr>

$(".deleteMe").click(function(){$(this).parent().parent().remove();});

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.