pjordanna
asked on
CHECKBOX STATE JAVASCRIPT FUNCTION
Hi Experts,
I am trying to produce a function which checks to see whether a checkbox within a form is either checked or unchecked. The function is to be called from the checkbox itself so when the checkbox is clicked the function works out whether the checkbox has just been checked or unchecked. WHat I have written so far is as follows:
<script language="javascript">
<!--
function travelIns() {
frmVal = document.detailsForm
if (frmVal.travelInsurance.ch ecked)
{
document.write("it's checked")
}
if (!frmVal.travelInsurance.c hecked)
{
document.write("it's not checked")
}
}
//-->
</script>
and the checkbox code is as follows:
<form action="confirmDetails.asp " method="post" name="detailsForm" onSubmit="return checkDetails()">
<input type="checkbox" name="travelInsurance" value="checkbox" onClick="travelIns();" checked>
</form>
It's not behaving as I expected. The function always returns "it's not checked" - any ideas?
PJORDANNA
I am trying to produce a function which checks to see whether a checkbox within a form is either checked or unchecked. The function is to be called from the checkbox itself so when the checkbox is clicked the function works out whether the checkbox has just been checked or unchecked. WHat I have written so far is as follows:
<script language="javascript">
<!--
function travelIns() {
frmVal = document.detailsForm
if (frmVal.travelInsurance.ch
{
document.write("it's checked")
}
if (!frmVal.travelInsurance.c
{
document.write("it's not checked")
}
}
//-->
</script>
and the checkbox code is as follows:
<form action="confirmDetails.asp
<input type="checkbox" name="travelInsurance" value="checkbox" onClick="travelIns();" checked>
</form>
It's not behaving as I expected. The function always returns "it's not checked" - any ideas?
PJORDANNA
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Two things:
You're writing to the document after it's already been established. This could be part of the problem. As the two people above mentioned, you should alert it instead. Like this:
function travelIns() {
frmVal = document.detailsForm;
if (frmVal.travelInsurance.ch ecked) {
alert("it's checked");
} else {
alert("it's not checked");
}
}
The second point I want to make is that you first test if it's checked, then again test if it's not checked, with a second if. This can be accomplished with one if statement, using the else clause.
Hope this helps.
You're writing to the document after it's already been established. This could be part of the problem. As the two people above mentioned, you should alert it instead. Like this:
function travelIns() {
frmVal = document.detailsForm;
if (frmVal.travelInsurance.ch
alert("it's checked");
} else {
alert("it's not checked");
}
}
The second point I want to make is that you first test if it's checked, then again test if it's not checked, with a second if. This can be accomplished with one if statement, using the else clause.
Hope this helps.
<HTML>
<HEAD>
<TITLE>Test Checkbox and Radio button </TITLE>
<SCRIPT LANGUAGE="javascript">
function showHide(sStatus)
{
//alert(document.frmTest.c hkShow1.ch ecked)
if (sStatus == 1)
{
if ( document.frmTest.chkShow1. checked)
alert("Yeh, first checkbox is checked")
else
alert("Ooopps, first checkbox is unchecked")
}
else
{
if ( document.frmTest.chkShow2. checked)
alert("Yeh, Second checkbox is checked")
else
alert("Ooopps, Second checkbox is unchecked")
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME = "frmTest">
<INPUT TYPE = "checkbox" NAME = "chkShow1" onClick = "showHide(1)" > Show Div area <br>
<INPUT TYPE = "checkbox" NAME = "chkShow2" onClick = "showHide(2)" > Hide Div area <br>
</FORM>
</BODY>
</HTML>
<HEAD>
<TITLE>Test Checkbox and Radio button </TITLE>
<SCRIPT LANGUAGE="javascript">
function showHide(sStatus)
{
//alert(document.frmTest.c
if (sStatus == 1)
{
if ( document.frmTest.chkShow1.
alert("Yeh, first checkbox is checked")
else
alert("Ooopps, first checkbox is unchecked")
}
else
{
if ( document.frmTest.chkShow2.
alert("Yeh, Second checkbox is checked")
else
alert("Ooopps, Second checkbox is unchecked")
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME = "frmTest">
<INPUT TYPE = "checkbox" NAME = "chkShow1" onClick = "showHide(1)" > Show Div area <br>
<INPUT TYPE = "checkbox" NAME = "chkShow2" onClick = "showHide(2)" > Hide Div area <br>
</FORM>
</BODY>
</HTML>
<!--
function travelIns() {
frmVal = document.detailsForm
if (frmVal.travelInsurance.ch
{
alert("it's checked")
}
if (!frmVal.travelInsurance.c
{
alert("it's not checked")
}
}
//-->
</script>
</head>
<body>
<form action="confirmDetails.asp
<input type="checkbox" name="travelInsurance" value="checkbox" onClick="travelIns();" checked>
</form>