javascript thread: SV: Re: Show more input fields only when checkbox is check in a form
>From: Doods Perea <dindo.perea@a...>
>Reply-To: "javascript" <javascript@p...>
>To: "javascript" <javascript@p...>
>Subject: [javascript] Re: SV: Re: Show more input fields only when checkbox
>is check in a form
>Date: Mon, 28 Oct 2002 15:49:14 +0300
>
>Hello all,
>
>Using Joe Fawcett's code, please see below how we can show/hide form
>elements based on the properties of checkboxes.
>(Joe -- many thanks. BTW, the typo I saw in the code was
>"getElementsName", which should be "getElementsByName").
>
>Regards,
>Doods
>
>
><script language="javascript">
>function updateDisplay()
>{
> var colCheckboxes = document.getElementsByName("chkType");
> var oChk;
> var sDisplayStyle;
> for (var i = 0; i < colCheckboxes.length; i++)
> {
> oChk = colCheckboxes[i];
> sDisplayStyle = (oChk.checked ? "" : "none");
> document.getElementById(oChk.value).style.display = sDisplayStyle;
> }
>}
></script>
><table>
><tr><td width=250>Payment Method</td>
> <input type="checkbox" name="chkType" value="elemCheck"
>onClick="updateDisplay(this)">Check
> <input type="checkbox" name="chkType" value="elemCredit"
>onClick="updateDisplay(this)">Credit Card</td>
> </tr>
><tr id = "elemCheck" STYLE="display:none">
> <td width=500>Content for Check.</td>
></tr>
>
><tr id = "elemCredit" STYLE="display:none">
> <td width=500>Content for Credit Card</td>
></tr>
></table>
>
If, for some reason, you need the value of the checkbox to be something
other than the id of the element in question then you could also add a cutom
attribute to the checkbox. For example add an attribute 'controlsElement' to
the checkboxes and set its value to the id of the element to show/hide. Then
change the code above to:
function updateDisplay()
{
var colCheckboxes = document.getElementsByName("chkType");
var oChk;
var sDisplayStyle;
for (var i = 0; i < colCheckboxes.length; i++)
{
oChk = colCheckboxes[i];
sDisplayStyle = (oChk.checked ? "" : "none");
document.getElementById(oChk.getAttribute("controlsElement")).style.display
= sDisplayStyle;
}
}
Joe
_________________________________________________________________
Get a speedy connection with MSN Broadband. Join now!
http://resourcecenter.msn.com/access/plans/freeactivation.asp