Create a table with collapsible and expandable rows using JQuery

So, you want to create a table with rows which should collapse and expandable as user clicks. This may sound like a difficult-to-achieve effect but it is not. I’m going to explain a very simple method to create a table with collapsible and expandable rows using JQuery.

Show / Hide tables row using jQuery

As you can see in the following images, we have a simple table with only three columns.
The rows are collapsed by default. If you click on the text ‘Average Salary of Web developer’, the following two rows will appear, and if you click on the same text again, rows will collapse/disappear.

Collapsed Table
Expanded Table

Add JQuery and OpenSans(Optional) in the head section of your document.
You can download JQuery from or can use CDN to link the JQuery. I prefer the latter option.

Now, create a simple table with 3 columns and 3 rows.

  		<tbody class="labels">
  				<td colspan="3">
  					<label for="salary">Average Salary of Web developer</label>
  					<input type="checkbox" name="salary" id="salary" data-toggle="toggle">
  		<tbody class="hide">
  				<td>Central America</td>


<label for=”salary”>Average Salary of Web developer</label>
<input type=”checkbox” name=”salary” id=”salary” data-toggle=”toggle”>

Data- attribute is new to Html5

According to w3specification: Custom data attributes are intended to store custom data private to the page or application, for

Let’s say you need an attribute which is not defined within the HTML tag. For example, you need an attribute to save the user name or email. There comes data-* attribute. You create custom attribute like data-username =”Someusername”, data-email = [email protected]

These custom data attributes consist of two parts:

Attribute Name: It should be prefixed with ‘data-‘.

Attribute Value: It can be any string.

In this case, we have to use a data attribute to refer it using JQuery.

To hide the element (checkbox) by default, set the property display to none using CSS. This would make the label/text clickable.

body{font-family: 'Open Sans', sans-serif; color:#333; font-size:14px; padding:50px;}
  label{display:inline-block;   }
  .table th,tr,td {
    padding: .75rem;
  td {
  border-bottom: 1px solid #dee2e6;
  tr {
    display: table-row;
    vertical-align: inherit;
    border-color: inherit;
 .label tr td label {
 	display: block;

 [data-toggle="toggle"] {
 	display: none;

The jQuery Code

    $(document).ready(function() {

  $('[data-toggle="toggle"]').click( function() {



$(this) would select the current element.

The parents() method will return all ancestor elements of the selected element.

$(this).parents().next(‘.hide’) will return the next sibling of the selected element’s parent, with .hide class.

The toggle() method toggles between show() and hide()

Related Articles

C# Keywords

C# Methods

C# Inheritance

Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers.

View all posts by Tutorials Panel →

Leave a Reply