Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <style>
    td, th {
      border: 1px solid black;
    }
    table {
      border-collapse: collapse;
    }
  </style>
    <script   
   src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
    </script>
    <script>
  
    function scrolify(tblAsJQueryObject, height){
        var oTbl = tblAsJQueryObject;
        // for very large tables you can remove the four lines below
        // and wrap the table with <div> in the mark-up and assign
        // height and overflow property  
        var oTblDiv = $("<div/>");
        oTblDiv.css('height', height);
        oTblDiv.css('overflow','scroll');               
        oTbl.wrap(oTblDiv);
        // save original width
        oTbl.attr("data-item-original-width", oTbl.width());
        oTbl.find('thead tr td').each(function(){
            $(this).attr("data-item-original-width",$(this).width());
        }); 
        oTbl.find('tbody tr:eq(0) td').each(function(){
            $(this).attr("data-item-original-width",$(this).width());
        });                 
        // clone the original table
        var newTbl = oTbl.clone();
        // remove table header from original table
        oTbl.find('thead tr').remove();                 
        // remove table body from new table
        newTbl.find('tbody tr').remove();   
        oTbl.parent().parent().prepend(newTbl);
        newTbl.wrap("<div/>");
        // replace ORIGINAL COLUMN width                
        newTbl.width(newTbl.attr('data-item-original-width'));
        newTbl.find('thead tr td').each(function(){
            $(this).width($(this).attr("data-item-original-width"));
        });     
        oTbl.width(oTbl.attr('data-item-original-width'));      
        oTbl.find('tbody tr:eq(0) td').each(function(){
            $(this).width($(this).attr("data-item-original-width"));
        });                 
    }
    $(document).ready(function(){
        scrolify($('#tblNeedsScrolling'), 160); // 160 is height
    });
    </script>
</head>
<body>
  <p>Something here</p>
  <p>Something here</p>
  <p>Something here</p>
  <div style="width:800px;border: 1px solid black;">
        <table width="100%" id="tblNeedsScrolling">
            <thead>
                <tr>
                  <th>Header 1</th>
                  <th>Header 2</th>
                  <th>Header 3</th>
                  <th>Header 4</th>
                  <th>Header 5</th>
                  <th>Header 6</th>
                  <th>Header 7</th>
                  <th>Header 8</th>
                  <th>Header 9</th>
                  <th>Header 10</th>
            </tr>
            </thead>
            <tbody>
                <tr>
                  <td>row 1, cell 1</td>
                  <td>row 1, cell 2</td>
                  <td>row 1, cell 3</td>
                  <td>row 1, cell 4</td>
                  <td>row 1, cell 5</td>
                  <td>row 1, cell 6</td>
                  <td>row 1, cell 7</td>
                  <td>row 1, cell 8</td>
                  <td>row 1, cell 9</td>
                  <td>row 1, cell 10</td>
              </tr>
              <tr>
                  <td>row 1, cell 1</td>
                  <td>row 1, cell 2</td>
                  <td>row 1, cell 3</td>
                  <td>row 1, cell 4</td>
                  <td>row 1, cell 5</td>
                  <td>row 1, cell 6</td>
                  <td>row 1, cell 7</td>
                  <td>row 1, cell 8</td>
                  <td>row 1, cell 9</td>
                  <td>row 1, cell 10</td>
              </tr>
              <tr>
                  <td>row 2, cell 1</td>
                  <td>row 2, cell 2</td>
                  <td>row 2, cell 3</td>
                  <td>row 2, cell 4</td>
                  <td>row 2, cell 5</td>
                  <td>row 2, cell 6</td>
                  <td>row 2, cell 7</td>
                  <td>row 2, cell 8</td>
                  <td>row 2, cell 9</td>
                  <td>row 2, cell 10</td>
              </tr>
              <tr>
                  <td>row 3, cell 1</td>
                  <td>row 3, cell 2</td>
                  <td>row 3, cell 3</td>
                  <td>row 3, cell 4</td>
                  <td>row 3, cell 5</td>
                  <td>row 3, cell 6</td>
                  <td>row 3, cell 7</td>
                  <td>row 3, cell 8</td>
                  <td>row 3, cell 9</td>
                  <td>row 3, cell 10</td>
              </tr>
              <tr>
                  <td>row 4, cell 1</td>
                  <td>row 4, cell 2</td>
                  <td>row 4, cell 3</td>
                  <td>row 4, cell 4</td>
                  <td>row 4, cell 5</td>
                  <td>row 4, cell 6</td>
                  <td>row 4, cell 7</td>
                  <td>row 4, cell 8</td>
                  <td>row 4, cell 9</td>
                  <td>row 4, cell 10</td>
              </tr>
              <tr>
                  <td>row 5, cell 1</td>
                  <td>row 5, cell 2</td>
                  <td>row 5, cell 3</td>
                  <td>row 5, cell 4</td>
                  <td>row 5, cell 5</td>
                  <td>row 5, cell 6</td>
                  <td>row 5, cell 7</td>
                  <td>row 5, cell 8</td>
                  <td>row 5, cell 9</td>
                  <td>row 5, cell 10</td>
              </tr>
              <tr>
                  <td>row 6, cell 1</td>
                  <td>row 6, cell 2</td>
                  <td>row 6, cell 3</td>
                  <td>row 6, cell 4</td>
                  <td>row 6, cell 5</td>
                  <td>row 6, cell 6</td>
                  <td>row 6, cell 7</td>
                  <td>row 6, cell 8</td>
                  <td>row 6, cell 9</td>
                  <td>row 6, cell 10</td>
              </tr>
              <tr>
                  <td>row 7, cell 1</td>
                  <td>row 7, cell 2</td>
                  <td>row 7, cell 3</td>
                  <td>row 7, cell 4</td>
                  <td>row 7, cell 5</td>
                  <td>row 7, cell 6</td>
                  <td>row 7, cell 7</td>
                  <td>row 7, cell 8</td>
                  <td>row 7, cell 9</td>
                  <td>row 7, cell 10</td>
              </tr>
              <tr>
                  <td>row 8, cell 1</td>
                  <td>row 8, cell 2</td>
                  <td>row 8, cell 3</td>
                  <td>row 8, cell 4</td>
                  <td>row 8, cell 5</td>
                  <td>row 8, cell 6</td>
                  <td>row 8, cell 7</td>
                  <td>row 8, cell 8</td>
                  <td>row 8, cell 9</td>
                  <td>row 8, cell 10</td>
              </tr>
              <tr>
                  <td>row 9, cell 1</td>
                  <td>row 9, cell 2</td>
                  <td>row 9, cell 3</td>
                  <td>row 9, cell 4</td>
                  <td>row 9, cell 5</td>
                  <td>row 9, cell 6</td>
                  <td>row 9, cell 7</td>
                  <td>row 9, cell 8</td>
                  <td>row 9, cell 9</td>
                  <td>row 9, cell 10</td>
              </tr>
            </tbody>
        </table>
    </div>
</body>
</html>
Output 300px

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

Dismiss x
public
Bin info
anonymouspro
0viewers