2

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();">
2
  • You say that wind and hail has to be checked... is this correct? Commented Mar 3, 2011 at 3:18
  • this test just checks for the hail Commented Mar 3, 2011 at 3:19

3 Answers 3

8

The 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 or false
  • Store a reference to theform for better performance
Sign up to request clarification or add additional context in comments.

8 Comments

What do you mean by store a reference to theform?
@shinjuo: the first line of code in my answer, var theform = document.theform;
Sorry it has been a long day. Thanks for the help
Also, the message being Hail checked is misleading as the if tests for Hail checked ONLY.
@shinjuo: long day makes for sloppy, frustating mistakes. I've been there. Go get some shut-eye, you'll write better code in the morning.
|
3
if(document.getElementById("hail").checked && !document.getElementById("wind").checked && !document.getElementById("tornado").checked) {
    alert("Hail Checked");
} else {
    alert("Nothing Selected");
}

2 Comments

I like the use of getElementById much better. Cleaner, and much less to chance with old old browsers, AHEM IE... AHEM...
Personally I'd never think of doing it the other way. Then again I'd also almost never not be using jQuery anyway…
2

In Javascript, the property's called checked, not Checked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.