Kovu Kovu - 5 months ago 8
jQuery Question

InsertAfter doesn't work

I try to insertAfter on a given element:

var design = "<span id='Umsatz'></span><span id='Paxe'></span><span id='UmsatzPaxe'></span>";
$.each(retValueStart, function (i, e) {
//Umsatz
var htmlRow = "";
htmlRow += "<tr>";
htmlRow += "<td>" + e.saison + "</td>";
htmlRow += "<td>" + e.gj + "</td>";
htmlRow += "<td>" + cMoney(e.summe_umsatz) + "</td>";
htmlRow += "<td>T€</td>";
htmlRow += "</tr>";
$(htmlRow).insertAfter($(design).find('#Úmsatz'));

//do other things


it simply doesn't work...

Ideas?

Answer

See the comments in code. Also, I've highlighted the modified code.

var design = "<span id='Umsatz'></span><span id='Paxe'></span><span id='UmsatzPaxe'></span>";
$(design).appendTo('body'); // Append to body first
// ^^^^^^^^^^^^^^^^^^^^^^^^

$.each(retValueStart, function(i, e) {
    //Umsatz
    var htmlRow = "";
    htmlRow += "<tr>";
    htmlRow += "<td>" + e.saison + "</td>";
    htmlRow += "<td>" + e.gj + "</td>";
    htmlRow += "<td>" + cMoney(e.summe_umsatz) + "</td>";
    htmlRow += "<td>T€</td>";
    htmlRow += "</tr>";
    $(htmlRow).insertAfter('#Umsatz'); // Don't need find here because ids are unique
    //                      ^^^^^^^^
});