Forum Moderators: open
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Table Row CSS Visibity Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">body { width:100%; height:100%; margin:0; }
#mainContent { position:relative; width:100%; height:100%; z-index:1; }
#row1 { font-size:10px; display:table-row; }
#row2 { font-size:10px; display:table-row; }
#row3 { font-size:10px; display:table-row; }</style>
<script type="text/javascript">settings = new Array("1","1","1","1");
function toggle(theRow) {
theRowID = "row" + theRow;
theElement = document.getElementById(theRowID);
if (settings[theRow] == "1") {
theElement.style.display = "none";
settings[theRow] = 0;
} else {
theElement.style.display = "table-row";
settings[theRow] = 1;
}
return false;
} // end toggle</script>
</head>
<body><div id="mainContent">
<table width="50%" cellpadding="0" cellspacing="0" border="1">
<tr id="row1">
<td><p class="row1"><a href="index.html" onclick="toggle(2); return false;">Row 1 Content</a></p></td>
</tr>
<tr id="row2">
<td><p>Row 2 Content</p></td>
</tr>
<tr id="row3">
<td><p>Row 3 Content</p></td>
</tr>
</table>
<br>
</div><!-- end mainContent -->
</body>
</html>
[blue]display:table-row;[/blue] This causes a script error, leading to the thread being abandoned, and the
href being followed (since the return false isn't executed). There is a way around the table-row issue that doesn't involve browser sniffing. This is:
Hiding: set element's direct
[blue]style.display[/blue] to "none" (as currently) [blue]""[/blue]. Effectively removing the attribute, and allowing the default style to take effect. Here's a function that uses this principle. We can do away with the lookup array,
settings, and just read the current display state from the element. style.display = "" -> evaluates as false -> the row is shown -> set to "none"
style.display = "none" -> evaluates true -> row is hidden -> set to "" Note that the function will go astray if any rows are
display:none; in stylesheet settings display:none; in inline style attributes though. using link:
onclick="return toggle(2);"
function toggle(iRow)
{
var rowStyle = document.getElementById("row"+iRow).style;
rowStyle.display = (rowStyle.display? "":"none");
return false;
}