I have an html page with a main tab control. I use javascript to show and hide tabs and sections of tab content. My problem is that if I change the visibility of an element inside one of the tab contents div to โhiddenโ and then back to โvisibleโ, the element seems to forget or lose its parent div container and remain visible, regardless of its original parental visibility.
To illustrate this, do the following example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript"> function hideTab(){ document.getElementById('tab1').style.visibility = 'hidden' } function showTab(){ document.getElementById('tab1').style.visibility = 'visible' } function hideContent(){ document.getElementById('tc1').style.visibility = 'hidden' } function showContent(){ document.getElementById('tc1').style.visibility = 'visible' } </script> </head> <body> <a href="javascript: hideTab()">Hide Tab</a><br /> <a href="javascript: showTab()">Show Tab</a><br /> <a href="javascript: hideContent()">Hide Content</a><br /> <a href="javascript: showContent()">Show Content</a><br /><br /> <div id="tab1" style="background:yellow;width:100px"> <div id='tc1'>Content Text</div> </div> </body> </html>
In IE8, click "Hide Tab", then "Show Tab", this works fine. On the tab shown, click Hide Content, then Show Content. This is also true. Now click "Hide Tab" again, and you will see that the tab disappears, but the contents are incorrectly left.
In IE8, the problem disappears when I use compatibility mode. Also, I noticed that if I delete the DOCTYPE element, I cannot replicate the problem.
In Chrome (my personal favorite), the problem remains constant, regardless of the DOCTYPE element. I have not tried this in firefox.
I am sure there is a very good reason for this behavior, I am also sure that for me there will be a simple solution so that my tabs work correctly. I look forward to your comments.
javascript dom visibility css
Sausagefingers
source share