Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Javascript Javascriptexamples JavaScript Hide Table Column

Related Tutorials


 
 

Share on Google+Share on Google+

JavaScript Hide Table Column

Advertisement
This page discusses - JavaScript Hide Table Column

JavaScript Hide Table Column

        

In this section, we are going to hide a table column using JavaScript.

In the given example, we have created a table consisting of four columns. Here we allow the user to enter the column number they want to hide. For this, we have created a function hideColumn() where the document.getElementById('tableID') method grabs the table reference and then retrieve all the rows by using getElementsByTagName('tr') method. The parameter 'cno' of the function retrieves the column number from the text field and the function make it hidden.

Following code allows to hide the entered column:

for (var row=0; row<rows.length;row++) {
var col = rows[row].getElementsByTagName('td')
col[cno].style.display=t;
}

Here is the code:

<html>
<h2>Hide Column</h2> <script>
function hideColumn(cno) {
var t = 'none';
var table = document.getElementById('tableID');
var rows = table.getElementsByTagName('tr');
for (var row=0; row<rows.length;row++) {
var col = rows[row].getElementsByTagName('td')
col[cno].style.display=t;
}
}
</script>
<table id='tableID' border="1">
<tr><td>SNO</td><td>NAME</td><td>ADDRESS</td><td>PHONE NO</td></tr>
<tr><td> 1</td><td> Tina </td><td> Mumbai</td><td> 1111111111</td></tr>
<tr><td> 2</td><td> Angelina</td><td> Delhi</td><td> 2222222222</td></tr>
</table>
<form>
Enter column no: <input type='text' name="cno">
<input type='button' onClick='javascript:hideColumn(cno.value);' value='hide'>
</form>
</html>

Output will be displayed as:

After entering the column no, when you click the button that column will get disappear:

Download Source Code:

        

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Discuss: JavaScript Hide Table Column  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
hadewych
March 25, 2012
doesnt work

This solution does not work because this line var col = rows[row].getElementsByTagName('td') will return an empty object. You cant ask for td members of table.rows ! FF6 IE8
DMCA.com