Link to home
Start Free TrialLog in
Avatar of Stefan Motz
Stefan MotzFlag for United States of America

asked on

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

Avatar of Brant Snow
Brant Snow

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>
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


});
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>
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

Avatar of Stefan Motz

ASKER

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.
<!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

ASKER CERTIFIED SOLUTION
Avatar of Brant Snow
Brant Snow

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much, this is perfect!