Search code examples
jqueryappendto

.appendTo and HTML literals


element.appendTo('<td>').appendTo('<tr>').appendTo('#ordersList')

Is this supposed to put the element inside a new td element, inside a new tr element inside the #ordersList element (was a table)?

Because I was just getting the naked element inside #ordersList with no wrapping tr/td.

This old site I was trying to troubleshoot seems to be using jQuery 1.4.1 - in case this is a known bug.


Solution

  • appendTo() returns the element you're operating on, so what you're actually doing is;

    1. Adding your element to a newly-created td.
    2. Detaching the element from the td and adding it to a newly created tr.
    3. Detaching it from the tr and attaching it to #ordersList

    What would work would be something like this;

    $('<td></td>').appendTo($('<tr></tr>')​​​​​​​​​.appendTo('#ordersList')).append(element);
    

    ... which sucks for readability.