I have been trying to check if a checkbox is checked for awhile. For some reason it is not working correctly it just always says nothing is selected.
Function
if(document.theform.hail.Checked==true && document.theform.wind.Checked==false && document.theform.tornado.Checked==false){
alert("Hail Checked");
}else{
alert("Nothing Selected");
}
Form
<form name="theform" action="<?php echo $PHP_SELF; ?>" method="post">
<div class="date">
From: <script type="text/javascript">DateInput('orderdate', true, 'YYMMDD')</script> To:<script type="text/javascript">DateInput('orderdatetwo', true, 'YYMMDD')</script>
</div>
<div class="checkBoxes">
<input id="hail" name="hail" type="checkbox" value="hail">Hail<br />
<input id="wind" name="wind" type="checkbox" value="wind">Wind<br />
<input id="tornado" name="tornado" type="checkbox" value="tornado">Tornado<br />
<input name="submit" type="submit" value="View Data" onClick="document.theform.action='<?php echo $PHP_SELF; ?>';">
<input name="submit" type="button" value="Create KML" onClick="generatorChoice();">
I have been trying to check if a checkbox is checked for awhile. For some reason it is not working correctly it just always says nothing is selected.
Function
if(document.theform.hail.Checked==true && document.theform.wind.Checked==false && document.theform.tornado.Checked==false){
alert("Hail Checked");
}else{
alert("Nothing Selected");
}
Form
<form name="theform" action="<?php echo $PHP_SELF; ?>" method="post">
<div class="date">
From: <script type="text/javascript">DateInput('orderdate', true, 'YYMMDD')</script> To:<script type="text/javascript">DateInput('orderdatetwo', true, 'YYMMDD')</script>
</div>
<div class="checkBoxes">
<input id="hail" name="hail" type="checkbox" value="hail">Hail<br />
<input id="wind" name="wind" type="checkbox" value="wind">Wind<br />
<input id="tornado" name="tornado" type="checkbox" value="tornado">Tornado<br />
<input name="submit" type="submit" value="View Data" onClick="document.theform.action='<?php echo $PHP_SELF; ?>';">
<input name="submit" type="button" value="Create KML" onClick="generatorChoice();">
Share
Improve this question
asked Mar 3, 2011 at 3:17
shinjuoshinjuo
21k24 gold badges76 silver badges104 bronze badges
2
- You say that wind and hail has to be checked... is this correct? – Zoidberg Commented Mar 3, 2011 at 3:18
- this test just checks for the hail – shinjuo Commented Mar 3, 2011 at 3:19
3 Answers
Reset to default 8The property should be in lowercase.
var theform = document.theform;
if(theform.hail.checked && !theform.wind.checked && !theform.tornado.checked) {
alert("Hail Checked");
} else {
alert("Nothing Selected");
}
Also, as the above code shows:
- No need to check explicitly against
true
orfalse
- Store a reference to
theform
for better performance
if(document.getElementById("hail").checked && !document.getElementById("wind").checked && !document.getElementById("tornado").checked) {
alert("Hail Checked");
} else {
alert("Nothing Selected");
}
In Javascript, the property's called checked
, not Checked
.