What is causing extra spacing to display when appending table rows with jquery

john k

I have some PHP code that creates the following table on a page, which is shown here:

echo '<table class="table" id="structures' . $i . '">';
    echo '<tr><td class="s_td"></td><td class="s_td"></td><td class="s_td"><input class="s_input" type="text" placeholder="Sub-bucket" name="sb' . $i . '[]"></td></tr>';
    echo '<tr>';
        if($draw_hyp == 1){
            echo '<td class="s_td"><input class="s_input" type="text" class="structure" placeholder="problem statement" name="hyp" id="hypo" /></td><td class="s_td"><input class="s_input" type="text" placeholder="Bucket" name="b[]"></td><td class="s_td"><input class="s_input" type="text" placeholder="Sub-bucket" name="sb' . $i . '[]"></td>';   
        }else{
            echo '<td class="s_td"></td><td class="s_td"><input class="s_input" type="text" placeholder="Bucket" name="b[]"></td><td class="s_td"><input class="s_input" type="text" placeholder="Sub-bucket" name="sb' . $i . '[]"></td>';
        }
    echo '</tr>';
    echo '<tr><td class="s_td"></td><td class="s_td"></td><td class="s_td"><input class="s_input" type="text" placeholder="Sub-bucket" name="sb' . $i . '[]"></td></tr>';
    echo '<tr><td class="s_td"></td><td class="s_td"></td><td class="s_td"><a href="#" class="add_sub_bucket" id="' . $i . '">Add</a></td></tr>';
echo '</table>';

Then I have some js which allow the user to add additional rows to the table, which is shown here:

$( "body" ).on( "click", ".add_sub_bucket", function(e) {
    e.preventDefault();
    var currentId = $(this).attr('id');
    $('#structures' + currentId).append('<tr><td class="s_td"></td>' + 
    '<td class="s_td"></td><td class="s_td"><input class="s_input" type="text" placeholder="Sub-bucket" name="sb' + currentId + '[]"></td></tr>' + 
    /*'<a href="#" class="remove_sub_bucket">x</a></td></tr>' + */
    '<tr><td class="s_td"></td><td class="s_td"></td><td class="s_td"><a href="#" class="add_sub_bucket" id="' + currentId + '">Add</a></td></tr>'
    );
    $(this).remove();
});

The js successfully adds the rows, but each additional row added appears to have double the spacing around it. The HTML from the initial PHP and the js reference the same .css file so I'm a bit puzzled as to why I get the extra spacing between rows from the js.

And here is rendered HTML of the table created by PHP code:

<form role="form" method="POST" id="inputStructure"> 
    <table class="table" id="structures0">
        <tr><td class="s_td"></td><td class="s_td"></td><td class="s_td"><input class="s_input" type="text" placeholder="Sub-bucket" name="sb0[]"></td></tr>
        <tr><td class="s_td"></td><td class="s_td"><input class="s_input" type="text" placeholder="Bucket" name="b[]"></td><td class="s_td"><input class="s_input" type="text" placeholder="Sub-bucket" name="sb0[]"></td></tr>
        <tr><td class="s_td"></td><td class="s_td"></td><td class="s_td"><input class="s_input" type="text" placeholder="Sub-bucket" name="sb0[]"></td></tr>
        <tr><td class="s_td"></td><td class="s_td"><a href="#" class="add_bucket" id="0">Add</a></td><td class="s_td"><a href="#" class="add_sub_bucket" id="0">Add</a></td></tr>
    </table>

Any pointers in the right direction would be much appreciated!

axel.michel

In your fiddle, you remove the "add" link, not the row containing the link, so you retrieve an empty row, that is your "double" space. Better to see, if you give the table a border.

$( "body" ).on( "click", ".add_sub_bucket", function(e) {
    e.preventDefault();
    var currentId = $(this).attr('id');
    $('#structures' + currentId).append('<tr><td class="s_td"></td>' + 
    '<td class="s_td"></td><td class="s_td"><input class="s_input" type="text" placeholder="Sub-bucket" name="sb' + currentId + '[]"></td></tr>' + 
    /*'<a href="#" class="remove_sub_bucket">x</a></td></tr>' + */
    '<tr><td class="s_td"></td><td class="s_td"></td><td class="s_td"><a href="#" class="add_sub_bucket" id="' + currentId + '">Add</a></td></tr>'
    );
   // this is the link not the row, you could do the following instead 
   // $(this).remove();
   $(this).closest('tr').remove();
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Appending rows into table using JQuery

From Dev

Cannot Figure out What is Causing Extra Spacing at the top of site

From Dev

Appending Rows to Nested HTML Table using Jquery

From Dev

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

From Dev

What is causing the extra padding?

From Dev

Removing extra spacing on top of a table

From Dev

CSS spacing issue - Not sure what is causing the space

From Dev

What is causing double rows, and how to fix when constrained by code

From Dev

Spacing in a html table between rows

From Dev

Remove spacing between table rows

From Dev

Remove spacing between table rows

From Dev

What is causing $ to be undefined? (JQuery)

From Dev

Struggle when appending rows to dataframe

From Dev

Appending jQuery selector variable into a table

From Dev

Appending row to table using Jquery

From Dev

JQuery exclude thead when reading rows in <table>

From Dev

How To remove Extra Line spacing between rows in CollectionView IOS?

From Dev

Spacing between 2 Rows in a HTML table

From Dev

Bugs appending rows to table and scrolling to bottom of page

From Dev

Views and appending table rows in backbone.js

From Dev

Spacing in <li> tags with display table-cell

From Dev

Unwanted spacing with css display and table-cell

From Dev

Unwanted spacing with css display and table-cell

From Dev

Cannot access jQuery when appending

From Dev

Cannot access jQuery when appending

From Dev

How can I get rid of extra vertical spacing in table cell?

From Dev

jQuery - Color table rows when checkbox is checked and remove when not

From Dev

jQuery appending remove button to list keeps adding an extra button

From Dev

Display extra rows (conditionally) separated by column name

Related Related

  1. 1

    Appending rows into table using JQuery

  2. 2

    Cannot Figure out What is Causing Extra Spacing at the top of site

  3. 3

    Appending Rows to Nested HTML Table using Jquery

  4. 4

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

  5. 5

    What is causing the extra padding?

  6. 6

    Removing extra spacing on top of a table

  7. 7

    CSS spacing issue - Not sure what is causing the space

  8. 8

    What is causing double rows, and how to fix when constrained by code

  9. 9

    Spacing in a html table between rows

  10. 10

    Remove spacing between table rows

  11. 11

    Remove spacing between table rows

  12. 12

    What is causing $ to be undefined? (JQuery)

  13. 13

    Struggle when appending rows to dataframe

  14. 14

    Appending jQuery selector variable into a table

  15. 15

    Appending row to table using Jquery

  16. 16

    JQuery exclude thead when reading rows in <table>

  17. 17

    How To remove Extra Line spacing between rows in CollectionView IOS?

  18. 18

    Spacing between 2 Rows in a HTML table

  19. 19

    Bugs appending rows to table and scrolling to bottom of page

  20. 20

    Views and appending table rows in backbone.js

  21. 21

    Spacing in <li> tags with display table-cell

  22. 22

    Unwanted spacing with css display and table-cell

  23. 23

    Unwanted spacing with css display and table-cell

  24. 24

    Cannot access jQuery when appending

  25. 25

    Cannot access jQuery when appending

  26. 26

    How can I get rid of extra vertical spacing in table cell?

  27. 27

    jQuery - Color table rows when checkbox is checked and remove when not

  28. 28

    jQuery appending remove button to list keeps adding an extra button

  29. 29

    Display extra rows (conditionally) separated by column name

HotTag

Archive