1

I trying to delete row from a table but somehow is not working. Could please let me know how can I solve this one ?

here is my html with table and javascript:

<tbody class="items">
    <tr>
        <td> Data 1 </td>
        <td> Data 2 </td>
    </tr>
</tbody>

<tbody id="test">
    <tr>
        <td> </td>
        <td> </td>
    </tr>
</tbody>

$(".items tr").click(function() {
    var value = parseInt($.trim(tableData[1]));
    $("#test").append(
        "<tr><td><input name='sm_invnumber[]' value='" + 
        $.trim(tableData[0]) + 
        "' style='width: 170px;' readonly ></td><td><input name='sm_amount[]' value='" +          
        $.trim(tableData[1]) + 
        "' style='width: 170px; text-align: right;' readonly ></td><td><span onclick='deleteRow(value, this)'> x </span> </td></tr>");
});

function deleteRow(value, row) {
    var i = row.parentNode.parentNode.rowIndex;
    document.getElementById('#test').deleteRow(i);
}

Here is the instruction that I am working with: Picking data by clicking on table (class= items) and place to them into table (id=test). there is a function with 'X'. I want to delete this row.

Helps are highly appreciated.

9
  • .remove() Commented Feb 25, 2014 at 11:43
  • 1
    document.getElementById('test') instead of document.getElementById('#test') Commented Feb 25, 2014 at 11:44
  • possible duplicate of How to delete dom element from jquery object Commented Feb 25, 2014 at 11:45
  • Why u mix up jquery and vanilla javascript? Commented Feb 25, 2014 at 11:45
  • Or $('#test') for the jquery way Commented Feb 25, 2014 at 11:45

3 Answers 3

3

You seem to making your life very difficult. Why not simply add a class to the cell that has the cross and use jQuery to remove its containing row. For example:

<table>
  <tbody id="test">
    <tr><td>Data 1</td><td class="delete">x</td></tr>
    <tr><td>Data 2</td><td class="delete">x</td></tr>
    <tr><td>Data 3</td><td class="delete">x</td></tr>
  </tbody>
</table>

$('.delete').click(function () {
  $(this).parent().remove();
});

Fiddle

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

1 Comment

Thank you. solutions are working well. But one is perfect for me which one I accepted.
1

try this

function deleteRow(value, span)
{
    $(span).closest("tr").remove();
 }

or

function deleteRow(value, span)
{
     var i=row.parentNode.parentNode.rowIndex;
     $(document.getElementById('test')).children(":eq("+i+")").remove();
 }

1 Comment

Thank you. solutions are working well. But one is perfect for me which one I accepted.
0

There are few poblems, try

jQuery(function () {
    var tableData = [1, 'test'];
    $(".items tr").click(function () {
        var value = parseInt($.trim(tableData[1]));
        $("#test").append(
            "<tr><td><input name='sm_invnumber[]' value='" + $.trim(tableData[0]) + "' style='width: 170px;' readonly ></td><td><input name='sm_amount[]' value='" + $.trim(tableData[1]) + "' style='width: 170px; text-align: right;' readonly ></td><td><span onclick='deleteRow(\"" + value + "\",this)'> x </span> </td></tr>");
    });
})

function deleteRow(value, row) {
    $(row).closest('tr').remove();
}

Demo: Fiddle, another version

Comments

Your Answer

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