JavaScript - Hide/Show table

Hi Experts,
I would like to show/hide the table around the text in it when clicking the "Show table" , "Hide Table" links.

The "CONTENT TO STAY" should be always showing, just the table around it has to disappear when the "Hide Table" link is clicked.

Is this possible to accomplish?

I appreciate your help.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show/Hide Content</title>
<script language="javascript" type="text/javascript">
function showHide(shID) {
   if (document.getElementById(shID)) {
      if (document.getElementById(shID+'-show').style.display != 'none') {
         document.getElementById(shID+'-show').style.display = 'none';
         document.getElementById(shID).style.display = 'block';
      }
      else {
         document.getElementById(shID+'-show').style.display = 'inline';
         document.getElementById(shID).style.display = 'none';
      }
   }
}
</script>
<style type="text/css">


   /* This CSS is used for the Show/Hide functionality. */
   .more {
      display: none;}

</style>
</head>
<body>


<table border="2" width="1000" align="center"><tr><td>

<div id="example" class="more">
<a href="#" id="example-hide"  onclick="showHide('example');return false;">Hide Table</a>
</div>
<div id="wrap">
<a href="#" id="example-show" onclick="showHide('example');return false;">Show Table</a>
</div>


CONTENT TO STAY

</td></tr></table>


</body>
</html>

Open in new window

romsomIT DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brant SnowCommented:
Sure can, in this case I am using jquery, which if you want to manipulate the dom you will want to use.  Here is the code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show/Hide Content</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<style type="text/css">



</style>
<script>
$(document).ready(function()
{
      
      
      $("#example-hide").click(function(){
      
            
            $("#mytable").attr("border",0);
      });
      
      $("#example-show").click(function(){
      
            $("#mytable").attr("border",2);
      });
});
</script>
</head>
<body>


<table border="2"  width="1000" align="center" id="mytable" class="mytableon" ><tr><td>

<div id="example" class="more">
<a href="#" id="example-hide"  onclick="showHide('example');return false;">Hide Table</a>
</div>
<div id="wrap">
<a href="#" id="example-show" onclick="showHide('example');return false;">Show Table</a>
</div>


CONTENT TO STAY

</td></tr></table>


</body>
</html>
0
Brant SnowCommented:
Basically as you can see, you had a border of 2 because you put the border html tag as 2

<table border="2"

so in this case you didnt need to set or change the CSS because you had directly made the table with a border of 2

The jquery is embedded from this link

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

$(document).ready(function()
{
 });    
     
The above code just means when the page is finished loading then the javascript will become active


 
      $("#example-hide").click(function(){
            $("#mytable").attr("border",0);
      });
The above here just says, when you click on something in HTML that has an ID of example-hide
then you should change the HTML that has an ID of mytable.  You will change its attribute of border to 0

     
      $("#example-show").click(function(){
     
            $("#mytable").attr("border",2);
      });
The above here just says, when you click on something in HTML that has an ID of example-hide
then you should change the HTML that has an ID of mytable.  You will change its attribute of border to 2


});
0
Brant SnowCommented:
Here is another way with CSS, where the jquery just adds or removes a class based upon clicks


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show/Hide Content</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<style type="text/css">
.borderon {
   border: 1px solid black;
}


</style>
<script>
$(document).ready(function()
{
      
      
      $("#example-hide").click(function(){
      
            
            $("#mytable").removeClass("borderon");
      });
      
      $("#example-show").click(function(){
      
            $("#mytable").addClass("borderon");
      });
});
</script>
</head>
<body>


<table  width="1000" align="center" id="mytable" class="borderon" ><tr><td>

<div id="example" class="more">
<a href="#" id="example-hide"  >Hide Table</a>
</div>
<div id="wrap">
<a href="#" id="example-show" >Show Table</a>
</div>


CONTENT TO STAY

</td></tr></table>


</body>
</html>
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Brant SnowCommented:
Of course if you want to do it without jquery and just normal javascript you can too, here is the code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show/Hide Content</title>
<script language="javascript" type="text/javascript">
function showHide(shID) {
   if (shID == "show")
   {
		
		document.getElementById('mytable').className = "borderon";
        
   }
      else {
         
         document.getElementById('mytable').className = "borderoff";
      }
   
}
</script>
<style type="text/css">

.borderon {
   border: 2px solid black;
}

.borderoff {
   border: 0px;
}

</style>
</head>
<body>


<table  width="1000" align="center" id="mytable" class="borderon"><tr><td>

<div id="example" class="more">
<a href="#" id="example-hide"  onclick="showHide('hide');return false;">Hide Table</a>
</div>
<div id="wrap">
<a href="#" id="example-show" onclick="showHide('show');return false;">Show Table</a>
</div>


CONTENT TO STAY

</td></tr></table>


</body>
</html>

Open in new window

0
romsomIT DeveloperAuthor Commented:
Thank you very much for your quick response. This is all very nice but I'm afraid I didn't explain what I'd like to accomplish with this. My goal is not to make the table border invisible. I would like to make the table like non-existent. The real life situation is that I have menus in the TD. Users would like to have the option to see only the CONTENT TO STAY visible (which is some database result in a different table). When they need to see the menus they would just click a "Show Menus" link on the page. The code below is something closer to my actual code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show/Hide Content</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<style type="text/css">



</style>
<script>
$(document).ready(function()
{


      $("#example-hide").click(function(){


            $("#mytable").attr("border",0);
      });

      $("#example-show").click(function(){

            $("#mytable").attr("border",2);
      });
});
</script>



</head>
<body>


<table border="2"  width="1000" align="center" id="mytable" class="mytableon" >
<tr><td colspan="2" align="center">DropDownMenu here</td></tr>
<tr><td width="200">FlyOutMenu here</td>

<td rowspan="2" align="center">

<div id="example" class="more">
<a href="#" id="example-hide"  onclick="showHide('example');return false;">Hide Table</a>
</div>
<div id="wrap">
<a href="#" id="example-show" onclick="showHide('example');return false;">Show Table</a>
</div>


CONTENT TO STAY

</td></tr>
<tr><td>Some other stuff here</td></tr>

</table>


</body>
</html>

Open in new window


Plus, when the "Show Table" link is clicked is should disappear, and only the "Hide Table" link should be visible. I am sorry for not making this clear when I posted the question.
0
romsomIT DeveloperAuthor Commented:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show/Hide Content</title>
<script language="javascript" type="text/javascript">
function showHide(shID) {
   if (document.getElementById(shID)) {
      if (document.getElementById(shID+'-show').style.display != 'none') {
         document.getElementById(shID+'-show').style.display = 'none';
         document.getElementById(shID).style.display = 'block';
      }
      else {
         document.getElementById(shID+'-show').style.display = 'inline';
         document.getElementById(shID).style.display = 'none';
      }
   }
}
</script>
<style type="text/css">


   /* This CSS is used for the Show/Hide functionality. */
   .more {
      display: none;}

</style>
</head>
<body>


<table border="2"  width="1000" align="center" id="mytable" class="mytableon" >
<tr><td colspan="2" align="center">DropDownMenu here</td></tr>
<tr><td width="200">FlyOutMenu here</td>

<td rowspan="2" align="center">

<div id="example" class="more">
<a href="#" id="example-hide"  onclick="showHide('example');return false;">Hide Table</a>
</div>
<div id="wrap">
<a href="#" id="example-show" onclick="showHide('example');return false;">Show Table</a>
</div>



CONTENT TO STAY<br />Everything in each TD should disappear when "Hide Table" is clicked.

</td></tr>
<tr><td>Some other stuff here</td></tr>

</table>


</body>
</html>

Open in new window

0
Brant SnowCommented:
Hey ronsom i think i understand what you are asking, firstoff you should research the difference between display and visibility, display will remove the structure entirely which will cause the elements to shift around where visibility keeps the structure in place just hides it,  anyway I prepared 2 examples, the first uses visibility mainly so it hides the menu but keeps the structure the same, the second uses display and consequently you will see everything shift

Visibility
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show/Hide Content</title>
<script language="javascript" type="text/javascript">
function showHide(shID) {
   if (shID == "hide")
   {
		
		document.getElementById('a1').className = "hide";
		document.getElementById('a2').className = "hide";
		document.getElementById('a3').className = "hide";
		document.getElementById('wrap').className = "reappear";
		document.getElementById('example').className = "disappear";
        document.getElementById('mytable').border=0;
   }
      else {
         
         document.getElementById('a1').className = "show";
		document.getElementById('a2').className = "show";
		document.getElementById('a3').className = "show";
		document.getElementById('wrap').className = "disappear";
		document.getElementById('example').className = "reappear";
		document.getElementById('mytable').border=2;
      }
   
}
</script>
<style type="text/css">


   /* This CSS is used for the Show/Hide functionality. */
   .hide {
      visibility: hidden;}
	  
	  .show{
	  visibility: visible;
	  }
	
	.disappear{
		display:none;
	}
	
	.reappear{
		display:block;
	}
	
</style>
</head>
<body>


<table border="2"  width="1000" align="center" id="mytable" class="mytableon" >
<tr><td colspan="2" align="center" id="a1">DropDownMenu here</td></tr>
<tr><td width="200" id="a2">FlyOutMenu here</td>

<td rowspan="2" align="center">

<div id="example" >
<a href="#" id="example-hide"  onclick="showHide('hide');return false;">Hide Table</a>
</div>
<div id="wrap" class="disappear">
<a href="#" id="example-show" onclick="showHide('show');return false;">Show Table</a>
</div>



CONTENT TO STAY<br />Everything in each TD should disappear when "Hide Table" is clicked.

</td></tr>
<tr><td id="a3">Some other stuff here</td></tr>

</table>


</body>
</html>

Open in new window



Display - in general i do not recommend hiding and showing td's, or tr's as the browser renders the td at the beginning and if you hide and show them they arent always rerendered correctly
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show/Hide Content</title>
<script language="javascript" type="text/javascript">
function showHide(shID) {
   if (shID == "hide")
   {
		
		document.getElementById('a1').className = "disappear";
		document.getElementById('a2').className = "disappear";
		document.getElementById('a3').className = "disappear";
		document.getElementById('wrap').className = "reappear";
		document.getElementById('example').className = "disappear";
        
   }
      else {
         
         document.getElementById('a1').className = "reappear";
		document.getElementById('a2').className = "reappear";
		document.getElementById('a3').className = "reappear";
		document.getElementById('wrap').className = "disappear";
		document.getElementById('example').className = "reappear";
      }
   
}
</script>
<style type="text/css">


   /* This CSS is used for the Show/Hide functionality. */
  
	
	.disappear{
		display:none;
	}
	
	.reappear{
		display:block;
	}
	
</style>
</head>
<body>


<table border="2"  width="1000" align="center" id="mytable" class="mytableon" >
<tr><td colspan="2" align="center" id="a1">DropDownMenu here</td></tr>
<tr><td width="200" id="a2">FlyOutMenu here</td>

<td rowspan="2" align="center">

<div id="example" >
<a href="#" id="example-hide"  onclick="showHide('hide');return false;">Hide Table</a>
</div>
<div id="wrap" class="disappear">
<a href="#" id="example-show" onclick="showHide('show');return false;">Show Table</a>
</div>



CONTENT TO STAY<br />Everything in each TD should disappear when "Hide Table" is clicked.

</td></tr>
<tr><td id="a3">Some other stuff here</td></tr>

</table>


</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
romsomIT DeveloperAuthor Commented:
Thank you very much, this is perfect!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.