Checkbox & if statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SimonPerry
    New Member
    • Jan 2007
    • 2

    #1

    Checkbox & if statements

    Hi, im relativley new to javascript and was wondering if anybody could assist me with a little problem i was having.

    quiet simply all i want to do is if a check box is checked then a value either 1 or 2 is placed in a field of a table, i have used the code below but when i hit the submit button nothing happens! im using dreamweaver and i placed the code below in the actual field section of the table hoping that the result would be wriiten there.
    Code:
    <script language="JavaScript" type="text/JavaScript">
    if (document.form1.chkBlue.checked)
     {
    document.write("1")
    }
     else if (document.form1.chkRed.checked)
     {
     document.write("2")
     }
    </script>
    if any body could help i would be so happy! thanks.
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    Try something like this:
    Code:
    <html>
    <head>
    <title>Test Some Check Boxes</title>
    <script type="text/javascript">
    function WriteOne()
    {
    document.write("1")
    }
    function WriteTwo()
    {
    document.write("2")
    }
    </script>
    
    </head>
    
    <body>
         <form method="post">
         Blue: <input type="checkbox" name="chkBlue" onclick="if(this.checked){WriteOne();}" /><br />
         Red: <input type="checkbox" name="chkRed" onclick="if(this.checked){WriteTwo();}" />
         </form>
         
    </body>
    </html>

    Comment

    • SimonPerry
      New Member
      • Jan 2007
      • 2

      #3
      Great, thank you very much!

      Comment

      Working...