Enable drupal_add_tabledrag on dynamically added rows I have used drupal_add_tabledrag to get Drupals sorting feature for a table, however, I cannot find any function to add the dragging feature to &lt;tr&gt; dynamically added with JavaScript to this table.

Does anyone know function Drupal provides to do this, or how you are supposed to do it?
In case anyone else stumbles across this, because of how Drupal handles forms, rows should be added in an AJAX callback, and the table should be replaced with the newly themed table. There are plenty of examples of adding new fields to a form using ajax.

If for some reason you are using the tabledrag feature outside of the form API (I don't know why you would), here is a quick method I wrote to add a row to a table, and make it draggable if the table is:

(function($){
    $.fn.addTableRow = function(row){
        // We can use this later to see if the row has been added to the table yet
        var added = false;

        // Get the ID of the table
        var tid = $(this).attr('id');

        // See if the table is set as a tableDrag table
        if(typeof Drupal.tableDrag[tid] != 'undefined'){

            // initialize order variable
            var order = false;

            // Set some variables for easier access
            var tableDrag = Drupal.tableDrag[tid];
            var settings = tableDrag.tableSettings;

            // Loop through the classes in the settings for the table 
            for(var aClass in settings){

                // Loop through the settings for the class
                for(var i in settings[aClass]){

                    // check which action the class uses, we could add the other actions
                    // here if we are worried about heirarchy or matching
                    switch(settings[aClass][i].action){
                        case 'order':

                            // if this class is used for ordering, assign it to order
                            order = aClass;
                            break;
                    }
                }
            }


            if(order){

                // find the max weight currently avaiable in the table
                var max = $(this).find('td.'+order).sort(function (a, b) {
                    return +a.value - +b.value;
                }).last().val();

                // Make sure the row has the draggable class
                row.addClass('draggable');

                // Add the row to the table, we don't do this earlier so that if
                // there is a value set in the weight field it is not included in the max
                // This could be done earlier, and setting the value could be ommitted
                // if you wanted to add the row to a specific locaiton in the table
                added = $(this).append(row);

                // Set the value of the order field to max+1
                $('.'+order, row).val(max+1);

                // Add the row so that tableDrag knows about it and can run it's
                // own hooks accordingly
                tableDrag.rowObject = new tableDrag.row(row);
                tableDrag.makeDraggable(row);
            }
        }

        // If the row wasn't added, order handling isn't used, just add it to the table.
        if(!added) $(this).append(row);
    }
})(jQuery);


Pretty straight forward usage:

var newRow = $('&lt;tr class="tabledrag-root"&gt;'+
                '&lt;td&gt;Column 1&lt;/td&gt;'+
                '&lt;td&gt;Column 2&lt;input class="sort-order" type="hidden" value="1"/&gt;&lt;/td&gt;'+
                '&lt;/tr&gt;');

$('#my_table').addTableRow(newRow);