<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
  </head>
  <body>
    <div class="container">
    
        <button class="btn btn-outline-primary btn-spec inline" id="ContentsChange">ContentsChange</button>
                    
      <table id="example" class="Table nowrap" width="100%">
        <thead>
          <tr>
            <th>Header1</th>
            <th>Header2</th>
            <th>Header3</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Body1</td>
            <td>Body2</td>
            <td>Body3</td>
          </tr>
        </tbody>
      </table>
       
    </div>
    <!-- Custom Javascript -->
    <script src="script.js"></script>
  </body>
</html>
 
body {
  font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
  margin: 0;
  padding: 0;
  color: #333;
  background-color: #fff;
}
 
$(document).ready( function () {
    
        $("#ContentsChange").click(function(){
            
        // Destroy old table
        var table = $('#example').DataTable();    
        table.clear().destroy();
        //table.destroy();
        
    
        // New Table Contents       
        const dataArray = [
            {
                NewHeader1: 'NewBody1',
                NewHeader2: 'NewBody2',
                NewHeader3: 'NewBody3',
                NewHeader4: 'NewBody4',
            },
        ];
        const headerCol = $('#example thead tr');
        Object.keys(dataArray[0]).forEach((d) => {
            headerCol.append(`<th>${d}</th>`);
        });
        $('#example').DataTable({
            data: dataArray,
            columns: Object.keys(dataArray[0]).map((d) => {
                return { data: d };
            }),
        });      
        
    });
} );
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers