Stefan Motz
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.
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>
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(f unction(){
$("#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(f unction(){
$("#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
});
<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(f
$("#mytable").attr("border
});
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(f
$("#mytable").attr("border
});
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(f unction(){
$("#mytable").removeClass( "borderon" );
});
$("#example-show").click(f unction(){
$("#mytable").addClass("bo rderon");
});
});
</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>
<!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(f
$("#mytable").removeClass(
});
$("#example-show").click(f
$("#mytable").addClass("bo
});
});
</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>
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:
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 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>
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.
ASKER
<!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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much, this is perfect!
<!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(f
$("#mytable").attr("border
});
$("#example-show").click(f
$("#mytable").attr("border
});
});
</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
</div>
<div id="wrap">
<a href="#" id="example-show" onclick="showHide('example
</div>
CONTENT TO STAY
</td></tr></table>
</body>
</html>