I am trying to toggle between two divs. For example, onload, I want the left content to show and the right content will remain hidden. If I click the right div, I want the left content to hide and the right content to appear and vise versa. I am very new to javascript but here is what I have. I can't seem to get it to work. Please help...
Here is my HTML code:
<div onclick="toggle_visibility('left');">
Left
</div>
<div onclick="toggle_visibility('right');">
Right
</div>
<div id="left" style="display: block;">
This is the content for the left side
<div>
<div id="right" style="display: none;">
This is the content for the ride side
</div>
Here is the Javascript I am using:
<script type="text/javascript">
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…