Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery add table row after the row which calling the jquery

Tags:

I've got a table.

<table id="servers" ...> ... {section name=i loop=$ownsites} <tr id="site_id_{$ownsites[i].id}"> ... <td>{$ownsites[i].phone}</td> <td class="icon"><a id="{$ownsites[i].id}" onClick="return makedeleterow(this.getAttribute('id'));" ...></a></td> </tr>        {/section} <tbody> </table> 

And this JavaScript.

<script type="text/javascript"> function makedeleterow(id)     {         $('#delete').remove();         $('#servers').append($(document.createElement("tr")).attr({id: "delete"}));         $('#delete').append($(document.createElement("td")).attr({colspan: "9", id: "deleter"}));         $('#deleter').text('Biztosan törölni szeretnéd ezt a weblapod?');         $('#deleter').append($(document.createElement("input")).attr({type: "submit", id: id, onClick: "return truedeleterow(this.getAttribute('id'))"}));         $('#deleter').append($(document.createElement("input")).attr({type: "hidden", name: "website_del", value: id}));     } </script> 

It's working fine, it makes a tr after the table's last tr and puts the info to it, and the delete function also works fine.

But I'd like to make this append AFTER the tr (with td class="icon") which is calling the script. How can I do this?

like image 485
Répás Avatar asked Apr 24 '10 10:04

Répás


People also ask

How can we append the first row in a table using jQuery?

append() / prepend() to Add Table Row in jQuery. To add a row in the table body using jQuery, we can use DOM inside insertion methods of append() or prepend() that adds an element to the suggested element's start or end. Here we will select the tbody element of table element with id="test" to add a row after it.

How do you add TR after current TR?

Basically, you create a copy from the tr element (which includes child nodes) and insert that copy after that element. Therefore, you need the . live binding to make sure that newly created a elements do also invoke that click handler.


1 Answers

You can use the .after() function in jQuery to append some content after another element.

So instead of

$("servers").append( ... ); 

you would use

$("#" + id + ).closest( "tr" ).after( ... ); 

or you could also use

$( ... ).insertAfter( $("#" + id ).closest( "tr" ) ); 

which is essentially equivalent.

See http://api.jquery.com/after/ for full details.

like image 103
Geoff Avatar answered Nov 08 '22 08:11

Geoff