How to combine two $(document).ready callbacks with jQuery?

Is it possible to combine these two JavaScript $(document).ready functions together? If so, how?

Code 1:

$(document).ready(function() {
    $('#example').dataTable( {
        "scrollY": 280,
        "scrollX": true,
        "pagingType": "simple",
        dom: 'T<"clear">lfrtip',
        tableTools: {
            "sSwfPath": "http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf"
        }
    });
});

Code 2:

$(document).ready(function() {
    var table = $('#example').DataTable();
    var tt = new $.fn.dataTable.TableTools( table );
    $( tt.fnContainer() ).insertBefore('div.dataTables_wrapper');
});

Upvotes: 0

Views: 116

Answers (2)

Cesarg2199
Cesarg2199

Reputation: 579

You should just be able to do this

$(document).ready(function() {

 var table = $('#example').DataTable();
 var tt = new $.fn.dataTable.TableTools( table );

 $( tt.fnContainer() ).insertBefore('div.dataTables_wrapper');


    $('#example').dataTable({

        "scrollY": 280,
        "scrollX": true,
        "pagingType": "simple",
        dom: 'T<"clear">lfrtip',
        tableTools: {
            "sSwfPath": "http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf"
        }



//End of document.ready
}); 

Hope this helps.

Upvotes: 1

Dan Lowe
Dan Lowe

Reputation: 56597

I assume you are asking if you can move the code inside the second ready() into the first, so you only call ready() one time? Yes, and it would look like this.

$(document).ready(function() {
    $('#example').dataTable( {

        "scrollY": 280,
        "scrollX": true,
        "pagingType": "simple",
        dom: 'T<"clear">lfrtip',
        tableTools: {
            "sSwfPath": "http://cdn.datatables.net/tabletools/2.2.2/swf/copy_csv_xls_pdf.swf"

        }

    });

    var table = $('#example').DataTable();
    var tt = new $.fn.dataTable.TableTools( table );

    $( tt.fnContainer() ).insertBefore('div.dataTables_wrapper');    
} );

Upvotes: 0

Related Questions