How to Wrap Tables with a Div Element Using jQuery

Last updated 3 weeks, 5 days ago | 29 views 75     5

Tags:- HTML JQuery

In web development, it's often useful to wrap a table inside a <div> — especially when you want to control scrolling, styling, or layout more precisely.
Instead of manually editing HTML, you can easily wrap tables dynamically using jQuery

In this article, we'll cover:

  • Step-by-step explanations,

  • Code snippets,

  • A full working example,

  • Plus tips and common pitfalls to avoid.

Let's dive in! 


Why Wrap a Table with a Div?

Before we jump into the how-to, let's quickly see why you might need this:

  • ✅ To add scrollbars to a table.

  • ✅ To apply custom CSS styles.

  • ✅ To group the table inside a container.

  • ✅ To make tables responsive on smaller screens.

Instead of touching the backend HTML, you can easily achieve this dynamically with jQuery.


Step-by-Step: How to Wrap a Table with a <div> Using jQuery

1. Create a Table in HTML

First, set up your basic HTML table:

<table id="myTable" border="1" cellpadding="10">
  <tr>
    <th>Name</th>
    <th>Age</th>
    <th>City</th>
  </tr>
  <tr>
    <td>John</td>
    <td>25</td>
    <td>New York</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>30</td>
    <td>Los Angeles</td>
  </tr>
</table>

2. Include jQuery

You must include jQuery in your page.
Add this <script> tag in the <head> or just before the closing </body> tag.

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

3. Write the jQuery Code

Use jQuery’s .wrap() function to wrap the table with a <div>.

Simple jQuery Code:

$(document).ready(function(){
    $("#myTable").wrap("<div class='table-wrapper'></div>");
});

Explanation:

  • $("#myTable") → Selects the table by its ID.

  • .wrap("<div class='table-wrapper'></div>") → Dynamically creates a new <div> around the table.

  • class="table-wrapper" → Adds a CSS class for styling if needed.

✅ That's it — your table is now wrapped inside a div!


Full Working Example

Here’s everything put together:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Wrap Table with Div using jQuery</title>
  <style>
    .table-wrapper {
      overflow-x: auto; /* Adds horizontal scroll if needed */
      max-width: 400px; /* Limit width to make scroll visible */
      border: 2px solid #ccc; /* Optional styling */
      padding: 10px;
      margin: 20px 0;
    }
  </style>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

<h2>Table Example</h2>

<table id="myTable" border="1" cellpadding="10">
  <tr>
    <th>Name</th>
    <th>Age</th>
    <th>City</th>
  </tr>
  <tr>
    <td>John</td>
    <td>25</td>
    <td>New York</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>30</td>
    <td>Los Angeles</td>
  </tr>
</table>

<script>
$(document).ready(function(){
    $("#myTable").wrap("<div class='table-wrapper'></div>");
});
</script>

</body>
</html>

What Happens?

  • The table is now wrapped inside a <div class="table-wrapper">.

  • The div can have custom styles (like scrollbars, padding, border, etc).


Pro Tips for Better Implementation

  • Target multiple tables:
    If you have more than one table, you can select them all:

    $("table").wrap("<div class='table-wrapper'></div>");
    
  • Wrap once only:
    If your code runs multiple times, make sure you don't double wrap your table.
    Check if already wrapped before wrapping:

    if (!$("#myTable").parent().hasClass("table-wrapper")) {
        $("#myTable").wrap("<div class='table-wrapper'></div>");
    }
    
  • Customize div easily:
    You can add id, multiple classes, inline styles, etc.

    $("#myTable").wrap("<div id='customWrapper' class='responsive-table' style='overflow-x:auto;'></div>");
    
  • Responsive Design:
    Adding overflow-x: auto in the wrapper div’s CSS makes the table responsive on mobile screens!


Common Pitfalls to Avoid

  • Missing jQuery library:
    Make sure you include jQuery before running your script, otherwise $ is not defined error will occur.

  • Trying to wrap elements already inside a div:
    If the table is already wrapped, re-wrapping could break your layout. Check with .parent() first.

  • Not using $(document).ready():
    Always wrap your code inside $(document).ready() to make sure the DOM is fully loaded before manipulating it.


Conclusion

Wrapping a table inside a div dynamically using jQuery is a simple yet powerful technique.
It helps you enhance layouts, add scrollbars, and apply custom designs without touching the backend or HTML templates manually.

✅ Quick
✅ Efficient
✅ Flexible

With a few lines of jQuery, your tables become more organized and responsive — ready for any modern web application!


See how to auto-wrap only large tables (like tables with more than 5 columns)
Click here