12

I have the following:

<table>
  <tr><td>author's first name</td><td>author's last name</td></tr>
  <tr><td><input type='text' name='first_name' /></td><td><input type='text' name='last_name' /></td></tr>
</table>
<a href='' onclick='return false;'>Add Author</a>

and each time I click the link tag to 'add author', I want to create an extra table row like the following:

  <tr><td><input type='text' name='first_name2' /></td><td><input type='text' name='last_name2'</td></tr>

I looked at .append() and I'm not sure exactly how to use it for my situation. And I'd like to be able to add infinite amount of new table rows. How do I do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Simon Suh
  • 10,599
  • 25
  • 86
  • 110

3 Answers3

14

Firstly, correct your HTML as:

<table class="authors-list">
    <tr>
        <td>author's first name</td><td>author's last name</td>
    </tr>
    <tr>
        <td>
            <input type="text" name="first_name" />
        </td>
        <td>
            <input type="text" name="last_name" />
        </td>
    </tr>
</table>
<a href="#" title="" class="add-author">Add Author</a>

(I have added indents only for visibility) and do not use inline JavaScript. Plus, be consistent, preferably use " (double quotes) for HTML attributes, ' (single quotes) for JavaScript strings.

Second, do something like that:

jQuery(function(){
    var counter = 1;
    jQuery('a.add-author').click(function(event){
        event.preventDefault();

        var newRow = jQuery('<tr><td><input type="text" name="first_name' +
            counter + '"/></td><td><input type="text" name="last_name' +
            counter + '"/></td></tr>');
            counter++;
        jQuery('table.authors-list').append(newRow);

    });
});

It will add a row to the table with each click on the link having class add-author (just to be sure no other link is influented), incrementing number at the end of the name of every inserted field's name by 1. The row will be inserted at the end of the table having class authors-list (same reason as with class for a link). See this jsfiddle as a proof.

Kailas
  • 7,350
  • 3
  • 47
  • 63
Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • I use single quotes as much as I can as it is one less keystroke in all of the code that I write, but if you would be kind enough to give me a specific reason why I should use double and single quotes as you have mentioned, I would love to change my coding preferences. I'm still new to the coding world (only been studying full time for about 8 months of php) and am happy to take in any good advice. – Simon Suh Nov 07 '11 at 22:39
  • 1
    @Qlidnaque: Just be consistent and use different types of quotes (different for JavaScript and different for HTML/CSS, so you minimize the chance of mistakes). In case of **PHP** it is more important, because when you use double quotes for strings that do not contain any variables, you may accidentally find yourself in a situation where some bug appears and you are unable to find it quick. In case of **JavaScript** this is more about choosing coding standard. I try to follow [MooTools' coding standard](https://github.com/mootools/mootools-core/wiki/syntax-and-coding-style-conventions). – Tadeck Nov 07 '11 at 22:47
  • @Tadeck how can i display dynamic data in table row which is coming from php? such as i have select boxes in all tds and there data is coming from db such as country, doctor specializations in extra. Thanks in advance. – Muddasir Abbas Jun 12 '15 at 11:11
  • @MuddasirAbbas: That is really wide topic - and the answers depends on whether you are asking about triggering the update, requesting from server, processing on the backend, retrieving, adding to table, or making it visually distinct. This answer really handles only "adding to table" part. If you will be more specific, I will try to point you to the right resources. – Tadeck Jun 12 '15 at 14:49
  • @Tadeck i am trying to append same row when user click on + icon and want to hide this row on click - icon. this functionality i am trying to create but i am confused how i can show php dynamic data in jquery append or after function. Thanks – Muddasir Abbas Jun 13 '15 at 18:43
  • Just wondering, why are you wrapping the string that contains the markup for the new row in `jQuery( ... )`? – Justin L. Aug 24 '16 at 21:36
3

Check it out here.

You'll probably want to increase the number in the name attributes in each iteration.

Shomz
  • 37,421
  • 4
  • 57
  • 85
0

Give an id to your table, suppose my_table and an id to the anchor a suppose my_button then try the below:

Your modified code:

<table id="my_table">
<tr><td>author's first name</td><td>author's last name</td></tr>
<tr><td><input type='text' name='first_name' /></td><td><input type='text' name='last_name' /></td></tr>
</table>
<a id="my_button" href='#'>Add Author</a>

And add the below script after including the jquery library in your page.

$(document).ready(function()
{
  new_row="<tr><td><input type='text' name='first_name2' /></td><td><input type='text' name='last_name2'</td></tr>";
  $("#my_button").click(function()
  {    
    $("#my_table").append(new_row);
    return false;

  }
}
Ghost-Man
  • 2,179
  • 1
  • 21
  • 25
  • -1 for not using `var` for newly created variables. It will make them global and pollute global namespace. Second reason is being inconsistent about quotes in HTML. Third reason would be not using [event.preventDefault()](http://api.jquery.com/event.preventDefault/) (which would increase code usability), but I am unable to give -3 instead of -1. Sorry. – Tadeck Nov 05 '11 at 05:48
  • Yes you are right. I just provided the minimal solution to create new rows using his html code. It will be problem to obtain values from his newly created elements, but as he mentioned he would like to create INFINITE rows, it seemed was interested in just testing. Anyway, your code is a practical solution. Thanks ! – Ghost-Man Nov 05 '11 at 05:55