dmegatool
2009-05-05, 22:41
Hey guys,
I don't know shit about javascript. I'm in a situation where I need to show/hide div when I click on a link. I'm using a script I've found and most of the time if fine. But the problem is that the divs are pilling up if I click on multiple links!
So I need a little bit help from real programmers :) It's pretty simple, I would need something that would show/hide the div I'm clicking on and hide all the others if they are shown. Could use an array with the id of the divs and get their display if it's none or not... I don't know.
I've found thi one that does what I need but it's hard to make it work in a real site situation. The show/hide divs would need to be included in a single div. I've tested with nested div and it didn't. Maybe you can tell me that this code is good and I need to try some more.
<!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=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.hide {
display:none;
}
-->
</style>
<script type="text/javascript">
function showHide(products) {
var el = document.getElementById('test');
var children = el.childNodes;
if (children[products].className == "hide") {
for(var i=0; i < children.length; i++) {
children[i].className = 'hide';
}
children[products].className = '';
}
else {
children[products].className = 'hide';
}
}
</script>
<style type="text/css">
</style>
</head>
<body>
<div id="test">
<div class="hide">
Content of Div 1
</div><div class="hide">
Content of Div 2
</div>
</div>
<a href="javascript:showHide('1');">Show Hide 1</a>
<a href="javascript:showHide('2');">Show Hide 2</a>
</body>
</html>
I don't know shit about javascript. I'm in a situation where I need to show/hide div when I click on a link. I'm using a script I've found and most of the time if fine. But the problem is that the divs are pilling up if I click on multiple links!
So I need a little bit help from real programmers :) It's pretty simple, I would need something that would show/hide the div I'm clicking on and hide all the others if they are shown. Could use an array with the id of the divs and get their display if it's none or not... I don't know.
I've found thi one that does what I need but it's hard to make it work in a real site situation. The show/hide divs would need to be included in a single div. I've tested with nested div and it didn't. Maybe you can tell me that this code is good and I need to try some more.
<!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=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.hide {
display:none;
}
-->
</style>
<script type="text/javascript">
function showHide(products) {
var el = document.getElementById('test');
var children = el.childNodes;
if (children[products].className == "hide") {
for(var i=0; i < children.length; i++) {
children[i].className = 'hide';
}
children[products].className = '';
}
else {
children[products].className = 'hide';
}
}
</script>
<style type="text/css">
</style>
</head>
<body>
<div id="test">
<div class="hide">
Content of Div 1
</div><div class="hide">
Content of Div 2
</div>
</div>
<a href="javascript:showHide('1');">Show Hide 1</a>
<a href="javascript:showHide('2');">Show Hide 2</a>
</body>
</html>