5

This is my js code:

<script>
function add(){
  $('#myTable tr:last').after('<tr><td>4<span onclick="del()">del row</span></td></tr>');
}
function del(){
  $(this).parent('tr').remove();
}
</script>

and html:

<table id="myTable" border="1">
    <tr><td>1</td></tr>
    <tr><td>2</td></tr>
    <tr><td>3</td></tr>
</table>

<span onclick="add()">add row</span>

My add button work nice but my del button not work. When del row clicked nothing happened.

2
  • 1
    It is here: '<tr><td>4<span onclick="del()">del row</span></td></tr>' stop downvote! Commented Jun 13, 2012 at 16:10
  • Couple of things. 1. this when using inline script is the window. 2. you need to replace parent with closest. 3. better use delegate event see my updated answer below. Commented Jun 13, 2012 at 16:15

5 Answers 5

4

Little change with your code

function add(){
  $('#myTable tr:last').after('<tr><td>4<span onclick="del(this)">del row</span></td></tr>');
}
    
function del(el) {
    $(el).closest('tr').remove()
}

Working sample


Another easier approach, not need radical change in your code

I think more easy will be that, if you add a class to the del row span and remove onclick like following:

function add(){
  $('#myTable tr:last').after('<tr><td>4<span class="delRow">del row</span></td></tr>');
}

and set delegate event handler like following:

$('#myTable').on('click', 'span.delRow', del);

and write your del() function like following:

function del() {
  $(this).closest('tr').remove();
}

Working Sample


And one important note

Don't forget to place your whole code within

$(document).ready(function() {..})

in short

$(function() {..})
Sign up to request clarification or add additional context in comments.

1 Comment

You don't have to delegate. You can bind the click event when the span is created dynamically.
1

Couple of things.

1. this when using inline script is the window.
2. you need to replace parent with closest.
3. better use delegate event

function del(){
     $(this).closest('tr').remove(); // closest!!
}    

$('#myTable').on('click', 'span', del);

Comments

1

Change your del function to:

function del(row){
  $(row).closest('tr').remove();
}​

and your add function to:

$('#myTable tr:last').after('<tr><td>4<span onclick="del(this)">del row</span></td></tr>');

jsFiddle example.

Comments

1

this is not going to be span, so it's not going to find the parent. I wouldn't create global functions for these at all nor would I use script attributes. I would rewrite it as:

<table id="myTable" border="1">
    <tr><td>1</td></tr>
    <tr><td>2</td></tr>
    <tr><td>3</td></tr>
</table>

<span id="add">add row</span>

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script>
$("#add").on('click', function () {
   $('#myTable tr:last').after(
      $("<tr>")
         .append($("<td>", {text: '4'})
            .append($("<span>", {text: 'del row'})
               .on('click', function () {
                  $(this).closest('tr').remove();
               })
            )
         )
   );
});
</script>

Comments

0

You should use:

$(this).parent('#myTable tr').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.