Show Hide Table Column Using JavaScript
Last Updated : Jul 1, 2023
IN - JavaScript HTML | Written & Updated By - Pragati
In this tutorial we will show you how to show hide table column using JavaScript.When user check or uncheck the checkbox a table column will hide or show respectively.You may also like sort table using jQuery.
CHECK OUT THIS TUTORIAL LIVE DEMO →
To Show Hide Table Column It Takes Only Two Steps:-
- Make a HTML file and define markup and scripting
- Make a CSS file and define styling
Step 1. Make a HTML file and define markup and scripting
We make a HTML file and save it with a name table.html
<html> <head> <link rel="stylesheet" type="text/css" href="table_style.css"> <script type="text/javascript"> function hide_show_table(col_name) { var checkbox_val=document.getElementById(col_name).value; if(checkbox_val=="hide") { var all_col=document.getElementsByClassName(col_name); for(var i=0;i<all_col.length;i++) { all_col[i].style.display="none"; } document.getElementById(col_name+"_head").style.display="none"; document.getElementById(col_name).value="show"; } else { var all_col=document.getElementsByClassName(col_name); for(var i=0;i<all_col.length;i++) { all_col[i].style.display="table-cell"; } document.getElementById(col_name+"_head").style.display="table-cell"; document.getElementById(col_name).value="hide"; } } </script> </head> <body> <div id="wrapper"> <div id="checkbox_div"> <p>Show Hide Column</p> <li><input type="checkbox" value="hide" id="name_col" onchange="hide_show_table(this.id);">Name</li> <li><input type="checkbox" value="hide" id="age_col" onchange="hide_show_table(this.id);">Age</li> <li><input type="checkbox" value="hide" id="city_col" onchange="hide_show_table(this.id);">City</li> </div> <table id="employee_table" align="center" cellpadding=10> <tr> <th>S.no</th> <th id="name_col_head">Name</th> <th id="age_col_head">Age</th> <th id="city_col_head">City</th> </tr> <tr> <td>1</td> <td class="name_col">Amit</td> <td class="age_col">26</td> <td class="city_col">Agra</td> </tr> <tr> <td>2</td> <td class="name_col">Rahul</td> <td class="age_col">22</td> <td class="city_col">Mumbai</td> </tr> <tr> <td>3</td> <td class="name_col">Manoj</td> <td class="age_col">31</td> <td class="city_col">Delhi</td> </tr> <tr> <td>4</td> <td class="name_col">Rashmi</td> <td class="age_col">25</td> <td class="city_col">Bhopal</td> </tr> <tr> <td>5</td> <td class="name_col">Shivam</td> <td class="age_col">23</td> <td class="city_col">Jaipur</td> </tr> </table> </div> </body> </html>
In this step we create a table and add some sample data for further working and create 3 checkbox one for each type of table column to hide and show the respective table column.
We create a function called 'hide_show_table()' to hide and show table column in this function we get the value of checkbox when clicked and check if the value is 'hide' it means we have to hide that particular table header and column and change that checkbox value to 'show' to show the hidden table header and column on next time.
You may also like add, edit and delete records from mysql table using jQuery, Ajax and PHP.
Step 2. Make a CSS file and define styling
We make a CSS file and save it with a name table_style.css
body { text-align:center; width:100%; margin:0 auto; padding:0px; font-family:helvetica; background-color:#FA8258; } #wrapper { text-align:center; margin:0 auto; padding:0px; width:995px; } #checkbox_div { width:375px; margin-left:309px; } #checkbox_div p { margin:5px; color:white; font-weight:bold; font-size:17px; } #checkbox_div li { color:white; display:inline-block; margin:5px; font-size:17px; font-weight:bold; } #checkbox_div input[type="checkbox"] { width:20px; height:20px; } #employee_table { color:#FE642E; background-color:white; text-align:center; } #employee_table th { border:1px dashed #FE642E; } #employee_table td { min-width:70px; border:1px dashed #FE642E; }
Thats all, this is how to show and hide table column using JavaScript and HTML. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
I hope this tutorial on show and hide table column using JavaScript helps you and the steps and method mentioned above are easy to follow and implement.