views:

329

answers:

6

I have a piece of the DOM that I'd like to insert into my page. Currently I'm just blindly using:

$(myblob).appendTo(someotherblob);

How do I do the same thing, but append myblob to the second to last row that is within someotherblob. someotherblob in this case is a table and I want to inject a row above the second to last one.

A: 

i recently put some code online that helps you deal with table modifications:

http://notetodogself.blogspot.com/2009/08/javascript-insert-table-rows-table-body.html

Also, you question is not too clear, but hopefully the link above will cover all bases

mkoryak
+7  A: 
$('#mytable tr:last').before("<tr><td>new row</td></tr>")
orip
A: 

i think you want to add a row per each row of the table beetween de second and the last row.

in this case try :

var rows=someotherblob.find("TR");
var nrows=rows.length;
    rows.slice(1,(nrows-2)).each(function(){
     $(this).append(myblob);
    })
TeKapa
A: 

Note that with before() the elements must already be inserted into the document (you can't insert an element before another if it's not in the page).

So you have to insert someotherblob first:

$('selector to insert someotherblob at')
    .append(someotherblob)
       .find('table tr:last')
          .prev()
          .before(myblob);
Sam Hasler
A: 

A bit of a shot in the dark, but I'm guessing that you've got a footer, or maybe even data entry, at the bottom of a table, and you want to add rows to the data, before the footer. In that case, you should restructure your table to this:

<table id="myTable">
    <thead>
     <tr><!-- header row(s) --></tr>
    </thead>
    <tfoot>
     <tr><!-- footer row(s) --></tr>
    </tfoot>
    <tbody>
     <tr><!-- exciting and possibly dynamically inserted data goes here --></tr>
    </tbody>
</table>

Yes, tbody does come after tfoot. Wierd, huh?

Then you can simply use this:

$("#myTable tbody").append(newrow);
Eric
A: 

As simple as this:

$("#TableName tr:last").prev().before("Hello World");

Fleents