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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With