JQuery append () method and debug chain order

I am trying to insert a new row into a table with jQuery. Can someone comment between two methods:

  • This works correctly:

    $('<tr>').append(row).appendTo(table);
    
  • This does not work, but it is not clear why?

    $(row).appendTo($('<tr>')).appendTo(table);
    
+3
source share
4 answers
$(row).appendTo($('<tr>').appendTo(table)); 

Give it a try. It looks like you just put your brackets in the wrong order.

+6
source
$(row).appendTo($('<tr>'))

This adds the line to the newly created <tr>. This returns a jQuery reference to row- NOT to the newly created<tr>

                          .appendTo(table);

Adds rowin table, removing it from the newly created <tr>.

Try this instead. He gets links directly.

$('<tr>').append(row).appendTo(table);
+2
source

. appendTo . tr, .


, : $(table).append($(row).wrap('<tr></tr>'));

, , . , .:)

+1

...

$(row).appendTo($('<tr>')).appendTo(table);

Set when you call a appendTo()second time is not what you think. It will be row.

This jsFiddle should be more clear.

+1
source

All Articles