Duplicate and Remove Table Rows through Jquery

user3968645

So I have managed to make it so you are able to add and remove table rows by clicking an icon on each table row.

My problem is though, let's say you add two (2) rows and then remove one (1) Then change your mind and add one (1) row to the table again. Well instead of getting one (1) row you get two (2). For some reason jQuery is bringing back that first row you removed plus the new one you just requested.

I opened up Google Chrome Inspector and did see that if you remove the row, it does in fact disappear from markup but yet still comes back when you add a new one.

My table is laid out like so:

<table class="table taskTable" style="text-align:center;">
<thead>
    <tr>
        <th>Task Name</th>

        <th>Est. Task Completion</th>

        <th>Est. Task Hours</th>

        <th>&nbsp;</th>
    </tr>
</thead>

<tbody>
    <tr>
        <td><input class="form-control" name="task_name[]" type="text"
        value=""></td>

        <td><input class="form-control" name="task_done[]" type="text"
        value=""></td>

        <td><input class="form-control" name="task_hour[]" type="text"
        value=""></td>

        <td style="vertical-align:middle; text-align:center;">
            <a class="removeTaskRow fa fa-trash-o" href="#" style=
            "color: #d9534f; font-style: italic; margin-right: 10px" title=
            "Remove Task"></a> <a class="addTaskRow fa fa-plus" href="#"
            style="color: #5cb85c; font-style: italic" title=
            "New Task"></a>
        </td>
    </tr>
</tbody>

I then have the following Javascript for adding and removing the table rows.

$(".addTaskRow").click(function(){
    addTableRow($('.taskTable tbody'));
});

function addTaskRow(){
    addTableRow($('.taskTable tbody'));
}

function removeTaskRow(){ 
    var par = $(this).parent().parent(); 
    par.remove(); 
};

function addTableRow(table){
    $(table).append(
        "<tr>"+
        "<td><input type='text' class='form-control' /></td>"+
        "<td><input type='text' class='form-control' /></td>"+
        "<td><input type='text' class='form-control' /></td>"+
        "<td style='vertical-align:middle; text-align:center;'><a href='#' style='margin-right: 10px; color: #d9534f' class='removeTaskRow' title='Remove Task'><i class='fa fa-trash-o'></i></a><a href='#' style='color: #5cb85c;' class='addTaskRow' title='New Task'><i class='fa fa-plus'></i></a></td>"+
        "</tr>"
    );

    $(".addTaskRow").bind("click", addTaskRow);
    $(".removeTaskRow").bind("click", removeTaskRow);
}

Why would jQuery be bringing back the previously deleted rows when you add a new one?

I have created a JSFiddle for you to see exactly what I am talking about. Try creating some new rows, deleting some, and then adding some back. JSFiddle: http://jsfiddle.net/8sj7n1h1/

Satpal

As you are creating elements dynamiclly.

You need to use Event Delegation. You have to use .on() using delegated-events approach.

Use

$('.taskTable tbody').on("click", ".addTaskRow", addTaskRow);
$('.taskTable tbody').on("click", ".removeTaskRow", removeTaskRow);

Also, Move these method outside addTableRow method

DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove duplicate rows from Html Table using jquery

From Dev

Remove duplicate rows from Html Table using jquery

From Dev

Jquery: How to remove duplicate HTML TABLE rows based on columns values

From Dev

JQuery - Looping through table rows

From Dev

SQL - Remove Duplicate Rows From Table

From Dev

Remove duplicate rows on many to many table (Mysql)

From Dev

LINQ - how to remove duplicate rows in table

From Dev

Remove duplicate rows on many to many table (Mysql)

From Dev

Find duplicate values on selected rows in table jquery

From Dev

jQuery search through table rows, hidden and visible

From Dev

Remove duplicate rows from MySQL table (ignore chars and whitespace)

From Dev

data.table : remove duplicate subset of rows for a given index value

From Dev

Remove duplicate rows from Data Table, but only if they match a list of strings

From Dev

Remove duplicate rows from table except for first timestamp of each day

From Dev

Cannot remove and then replace rows in a table with JQuery

From Dev

JQuery click event to add or remove table rows

From Dev

jQuery Datatable: don't remove table rows

From Dev

Jquery to remove all the rows from closest table

From Dev

Add and remove class in table rows JQuery

From Dev

Remove duplicate rows in powerpivot

From Dev

how to remove duplicate rows

From Dev

Dont want to append the duplicate rows in the table using jquery

From Dev

Find duplicate rows in a table

From Dev

Delete duplicate rows in a table

From Dev

remove duplicate elements through javascript

From Java

Move through rows of a table using arrows keys in jQuery

From Dev

Jquery loop through all the rows in a table without first row

From Dev

Jquery loop through table rows and get nth child value

From Dev

Jquery loop through all the rows in a table without first row

Related Related

  1. 1

    Remove duplicate rows from Html Table using jquery

  2. 2

    Remove duplicate rows from Html Table using jquery

  3. 3

    Jquery: How to remove duplicate HTML TABLE rows based on columns values

  4. 4

    JQuery - Looping through table rows

  5. 5

    SQL - Remove Duplicate Rows From Table

  6. 6

    Remove duplicate rows on many to many table (Mysql)

  7. 7

    LINQ - how to remove duplicate rows in table

  8. 8

    Remove duplicate rows on many to many table (Mysql)

  9. 9

    Find duplicate values on selected rows in table jquery

  10. 10

    jQuery search through table rows, hidden and visible

  11. 11

    Remove duplicate rows from MySQL table (ignore chars and whitespace)

  12. 12

    data.table : remove duplicate subset of rows for a given index value

  13. 13

    Remove duplicate rows from Data Table, but only if they match a list of strings

  14. 14

    Remove duplicate rows from table except for first timestamp of each day

  15. 15

    Cannot remove and then replace rows in a table with JQuery

  16. 16

    JQuery click event to add or remove table rows

  17. 17

    jQuery Datatable: don't remove table rows

  18. 18

    Jquery to remove all the rows from closest table

  19. 19

    Add and remove class in table rows JQuery

  20. 20

    Remove duplicate rows in powerpivot

  21. 21

    how to remove duplicate rows

  22. 22

    Dont want to append the duplicate rows in the table using jquery

  23. 23

    Find duplicate rows in a table

  24. 24

    Delete duplicate rows in a table

  25. 25

    remove duplicate elements through javascript

  26. 26

    Move through rows of a table using arrows keys in jQuery

  27. 27

    Jquery loop through all the rows in a table without first row

  28. 28

    Jquery loop through table rows and get nth child value

  29. 29

    Jquery loop through all the rows in a table without first row

HotTag

Archive