0

I am using to add a some rows using javascript. I was able to achieve that using:

$('#order_basket > tbody:last').append('<tr><td><b>'+Book+
             '</td><td>'+Type+'</td><td>'+'<span onclick="removeRow()"'+
             'class="label label-important">Remove</span></td></tr>');      
        return false;
});

Now i also want to be able to remove that row using javascript too. In the able code i have a label named as Remove which is calling another function in my javascript that is being used to remove the selected row. However i am unable to achieve that. The code i am using to remove the row is:

function removeRow() {
    $(this).parent().remove();
    return false;
}

Any help will be appreciated!

4
  • How are you calling the removeRow function ? Commented Feb 26, 2012 at 17:38
  • 1
    using onclick, its there in the first block of code <span onclick="removeRow()". Commented Feb 26, 2012 at 17:39
  • Didn't this work? : Delete a table row with javascript / jquery Commented Feb 26, 2012 at 17:42
  • @Namit: Ok got you, see my answer, hope it helps. Commented Feb 26, 2012 at 17:43

1 Answer 1

3

You have to pass this inside the onclick event as follows:

'<span onclick="removeRow(this)"'

And change the function to:

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

Instead of passing the event as an attribute via the HTML, you can also use:

        $('#order_basket > tbody:last').append('<tr><td><b>' + Book +
             '</td><td>'+Type+'</td><td>'+'<span'+
             'class="label label-important remove">Remove</span></td></tr>');      
        return false;
});

$('#order_basket > tbody:last').on('click', 'span.remove', function() {
    $(this).closest('tr').remove();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Works if i change the $(this).closest('row').remove(); to $(this).closest('tr').remove();

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.