Edit new created row before adding to datatables, with jquery

Edit new created row before adding to datatables, with jquery

itajackassitajackass Posts: 162Questions: 48Answers: 3

I've a script that insert a new row inside a table with ajax:

I need to edit a <select> inside the new added row (plain text) and set a selected value. In this example 5. But with my code it doesn't work: $(tr).find(".select").val(idSelect);

What't the correct way?

var idSelect = "5";

 $.ajax({
                    type: "POST",
                    async: false,
                    dataType:"text",
                    url: "ajax/get-new-row.php",
                    data: { },
                    success: function(data) {

                        var tr = tabella.row.add(
                            $(data)[0]
                        );

                        $(tr).find(".select").val(idSelect); // doesn't work
                                                    tabella.row(tr).draw( false ).node();
                        tabella.rows().invalidate('dom').draw(false);

}
});

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    edited March 2018

    var tr is not a node. You'd need to use:

    $( tr.node() ).find( 'select' )...
    

    instead. As the row.add() documentation states, it returns a DataTables API instance with the newly added row selected.

    Allan

This discussion has been closed.