0

I am trying to dynamically add rows to a specific table on my site which has many tables. I use the following jquery to perform that task. However, in the appendto method if I put in the passed variable of addrow it adds the row to all of my tables but if I specify with text the id it works fine.

function addrow(id){

    $("<tr><td><input type = 'text' value = 'Equipo 1' /></td><td></td><td></td><td></td><td></td></tr>").appendTo("[id] tbody");  

}

id is "#5a7"

this way a row gets added to all of my tables

function addrow(id){

    $("<tr><td><input type = 'text' value = 'Equipo 1' /></td><td></td><td></td><td></td><td></td></tr>").appendTo("#5a7 tbody");

}

If I do it like this it works fine and only adds the row to that table.

Any ideas?

2 Answers 2

1

Change your addrow function like this

function addrow(id){
    $("<tr><td><input type = 'text' value = 'Equipo 1' /></td><td></td><td></td><td></td><td></td></tr>").appendTo("#"+id+" tbody"); 
}

working sample http://jsfiddle.net/5SVAJ/11/

ID's of your Element should be unique to elements.

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

1 Comment

That worked great, any idea why adding the rows in this manner expands my table?
0

This is what I do:

$('#my_table > tbody:last').find('tr:last').after(/*data to append*/);

2 Comments

this inserts it at the end of my very last table, I want it to be inserted in the table for that particular id.
#my_table > tbody:last points to the last table body of id #my_table. Next, we find the last tr (find('tr:last')). Finally, after the last tr in the last body, we append a new line. THe accepted answer is vary similar, except if you should happen to have more than one tbody, in the accepted answer you will append to the FIRST tbody, in this answer you will append to the last.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.