"P.Hill" <go************@xmissionREMOVE.com> wrote in message
news:cq**********@news.xmission.com...
I have this simple bit of code that when you check a checkbox and it
displays or hides two other fields (well actually two parts of the
document with two different IDs. Pretty straight forward... I
thought. The fields come and go, but something happens to the table formatting;
on NS 7.1 and Firefox 1.0 it seems to miscount or hide the number of
TD tags or something else confusing.
Can anyone tell me what I'm doing wrong?
-Paul
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1"> <script language="JavaScript" >
<script type="text/javascript">
function hideOn( checkbox, offId, onId ) {
uncheckedDiv = document.getElementById(offId);
checkedDiv = document.getElementById(onId);
if ( checkbox.checked ) {
checkedDiv.style.visibility = 'visible';
checkedDiv.style.display = "block";
A <tr> is not display:block, it is display:table-row, but IE doesn't
support that, so the best approach is to set the display style to an
empty string, which will make the element use it's default value for
that attribute:
checkedDiv.style.display = "";
You also don't need to change both the -visibility- and -display- style
attributes, only -display- needs to be toggled.
uncheckedDiv.style.visibility = 'hidden';
uncheckedDiv.style.display = "none";
} else {
checkedDiv.style.visibility = 'hidden';
checkedDiv.style.display = "none";
uncheckedDiv.style.visibility = 'visible';
uncheckedDiv.style.display = "block";
uncheckedDiv.style.display = "";
}
}
</script>
</head>
<body>
<form>
<table align="center">
<tr>
<td nowrap align="right" valign="top">
</td>
<td nowrap align="left" valign="top">As textarea (or as text)
<input type="checkbox" name="docTxt" onChange="hideOn(
this.form.docTxt, 'asTextArea', 'asText' );"/>
<input type="checkbox"> has no onChange event, it has an onclick event.
You also only need to pass -this-, you don't need a fully qualified
reference to the element you are triggering the event on:
<input type="checkbox" name="docTxt" onchange="hideOn(this,
'asTextArea', 'asText');">
</td>
<tr>
Should be </tr>
<tr id="asTextArea" style="visibility:visible" valign="baseline">
<td nowrap align="right" valign="top">TextArea:</td>
<td align="left" valign="top">
<textarea name="body" cols="50" rows="5"></textarea></td>
</tr>
<tr id="asText" style="visibility:hidden">
<td nowrap align="right" valign="top">Text:</td>
<td align="left" valign="top"><input type="text" name="bodyFile"
size="40" /></td>
</tr>
</table>
</form>
</body>
</html>
Putting it all together:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function hideOn(checkbox, offId, onId) {
var uncheckedDiv = document.getElementById(offId);
var checkedDiv = document.getElementById(onId);
if (checkbox.checked) {
checkedDiv.style.display = "";
uncheckedDiv.style.display = "none";
} else {
checkedDiv.style.display = "none";
uncheckedDiv.style.display = "";
}
}
</script>
</head>
<body>
<form>
<table align="center">
<tr valign="top">
<td nowrap="nowrap" align="right"> </td>
<td nowrap="nowrap">As textarea (or as text)
<input type="checkbox" name="docTxt" onclick="hideOn(this,
'asTextArea', 'asText' );">
</td>
</tr>
<tr id="asTextArea" valign="top">
<td nowrap align="right">TextArea:</td>
<td><textarea name="body" cols="50" rows="5"></textarea></td>
</tr>
<tr id="asText" style="display:none;" valign="top">
<td nowrap align="right">Text:</td>
<td><input type="text" name="bodyFile" size="40"></td>
</tr>
</table>
</form>
</body>
</html>
Tested and working in Firefox 1.0, IE 6 and Opera 7.54u1 (watch for
word-wrap).
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ -
http://jibbering.com/faq