adding <tr> to table using jquery + keep jquery function on added rows

Chris

I got a table where I want to add additional rows when clicking on a selector on the end of the row. I found a solution here: jQuery Clone table row

The pure clone of the row is perfekt. But afterwords I can't clone the clone with clicking on the selector of the end of the cloned row. I only does clone when I klick on a selector of a row which is hardcoded and not cloned.

Here is the code of the html/php:

            <table  class="table table-bordered table-hover">
            <tr>
                <th id="col1">Datum / Anzahl</th>
                <th id="col2">Text</th>
                <th id="col3">Einzelpreis</th>
                <th id="col4">Gesamtpreis</th>
            </tr>

        <!-- Einzelpositionen -->
            <tr>
                <td align="left" class="editCell">
                    <span class="input">
                        <input type="text" value="<?php echo $eventDatum ?>">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                    </span>
                </td>
                <td align="left" class="editCell">
                    <span class="input">
                        <input type="text" value="<?php echo $eventTxt ?>">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                    </span>
                </td>
                <td align="right" class="editCell">
                    <span class="input">
                        <input type="text" text-align="right" value="<?php echo $ePreis ?>">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                    </span>&nbsp;<?=$waehrung ?>
                </td>
                <td align="right" class="editCell">
                    <span class="input">
                        <input type="text" text-align="right" value="<?php echo $gPreis ?>">
                        <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                    </span>&nbsp;<?=$waehrung ?>

                </td>
                <td class="no-border">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true">plus</span>
                </td>
            </tr>


        <!-- Summen und MwSt -->    
            <tr>
                <td align="right" colspan="3">Zwischensumme</td>
                <td align="right">
                    <span class="text"><?php echo $nettoSum ?></span>
                    <span class="input">
                        <input type="hidden" text-align="right" value="<?php echo $nettoSum ?>">
                    </span>&nbsp;<?=$waehrung ?>
                </td>
            </tr>
            <tr>
                <td align="right" colspan="3">Mehrwertsteuer 19%</td>
                <td align="right">
                    <span class="text"><?php echo $vat ?></span>
                    <span class="input">
                        <input type="hidden" text-align="right" value="<?php echo $vat ?>">
                    </span>&nbsp;<?=$waehrung ?>
                </td>
            </tr>
            <tr>
                <th style="text-align: right !important" colspan="3">Rechnungsbetrag</th>
                <th style="text-align: right !important">
                    <span class="text"><?php echo $bruttoSum ?></span>
                    <span class="input">
                    <input type="hidden" text-align="right" value="<?php echo $bruttoSum ?>">
                    </span>&nbsp;<?=$waehrung ?>
                </th>
            </tr>
        </table>

And here the code of the jquery:

    $('.glyphicon-plus').click(function() {
    var $tr    = $(this).closest('tr');
    var $clone = $tr.clone();
    $clone.find(':text').val('');
    $clone.find('span.text').after('-kopie');
    $tr.after($clone);

});

My fist entry on fiddle will hopefully work ;-) http://jsfiddle.net/8v35m2c1/

Hope to get some ideas. The code are just parts of my original files ;-)

Cheers, Chris

Rhumborl

You need to use the on function instead of click to ensure that new rows also get the event handler:

$('table').on('click', '.glyphicon-plus', function() {
    var $tr    = $(this).closest('tr');
    var $clone = $tr.clone();
    $clone.find(':text').val('');
    $clone.find('span.text').after('-kopie');
    $tr.after($clone);
});

Update fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding rows to table using jquery

From Dev

Adding rows to table using jquery

From Dev

Calculate automatically in a table using jquery when adding or deleting rows

From Dev

jQuery click event for dynamically added tr's in table with specific ID

From Dev

Autocomplete on dyamically added rows Using Jquery

From Dev

Adding new rows to table on clicking button in JQuery

From Dev

jQuery, adding multiple table rows is not working correctly

From Dev

Adding or deleting table rows with MySQL, jQuery and Ajax

From Dev

Adding table rows and column dynamically with jQuery

From Dev

When appending tr to table thead using jquery each ,results blank rows

From Dev

Expanding and collapsing table row (tr) using jquery

From Dev

Expand even tr tags in table using jquery

From Dev

Click event is not working when <tr> added dynamically using jQuery ajax

From Dev

Appending rows into table using JQuery

From Dev

jQuery: Adding td element to a tr

From Dev

Copy table rows in multiple table using JQUERY

From Dev

unable to delete newly added rows on the table with dataTable jquery

From Dev

JQuery - How to implement event listener for dynamically added droppable table rows?

From Dev

unable to delete newly added rows on the table with dataTable jquery

From Dev

jquery doesn't work on dynamically added table rows

From Dev

jQuery Fire a custom event on table when new rows are added

From Dev

adding class to jquery added numbers

From Dev

counting no of specific rows in a table using jquery(.each() function) also using .attr() function

From Dev

Adding new row to the table using javascript / jquery

From Dev

Adding the numbers in table row using jquery

From Dev

Adding a td for existing table using jQuery

From Dev

Adding data to the table with the checkbox checked using jquery

From Dev

Adding the numbers in table row using jquery

From Dev

Adding a td for existing table using jQuery

Related Related

  1. 1

    Adding rows to table using jquery

  2. 2

    Adding rows to table using jquery

  3. 3

    Calculate automatically in a table using jquery when adding or deleting rows

  4. 4

    jQuery click event for dynamically added tr's in table with specific ID

  5. 5

    Autocomplete on dyamically added rows Using Jquery

  6. 6

    Adding new rows to table on clicking button in JQuery

  7. 7

    jQuery, adding multiple table rows is not working correctly

  8. 8

    Adding or deleting table rows with MySQL, jQuery and Ajax

  9. 9

    Adding table rows and column dynamically with jQuery

  10. 10

    When appending tr to table thead using jquery each ,results blank rows

  11. 11

    Expanding and collapsing table row (tr) using jquery

  12. 12

    Expand even tr tags in table using jquery

  13. 13

    Click event is not working when <tr> added dynamically using jQuery ajax

  14. 14

    Appending rows into table using JQuery

  15. 15

    jQuery: Adding td element to a tr

  16. 16

    Copy table rows in multiple table using JQUERY

  17. 17

    unable to delete newly added rows on the table with dataTable jquery

  18. 18

    JQuery - How to implement event listener for dynamically added droppable table rows?

  19. 19

    unable to delete newly added rows on the table with dataTable jquery

  20. 20

    jquery doesn't work on dynamically added table rows

  21. 21

    jQuery Fire a custom event on table when new rows are added

  22. 22

    adding class to jquery added numbers

  23. 23

    counting no of specific rows in a table using jquery(.each() function) also using .attr() function

  24. 24

    Adding new row to the table using javascript / jquery

  25. 25

    Adding the numbers in table row using jquery

  26. 26

    Adding a td for existing table using jQuery

  27. 27

    Adding data to the table with the checkbox checked using jquery

  28. 28

    Adding the numbers in table row using jquery

  29. 29

    Adding a td for existing table using jQuery

HotTag

Archive