Please help with my javascript code!

Discussion in 'JavaScript' started by Luke Watson, Feb 28, 2011.

  1. #1
    Hey guys i'm stuck on one thing in my javascript code. I have a "clear" button which when pressed erases any text in any of the fields. All that is working fine but i also have the 5 operators which are +, -, x, / and %. I want it so if the user clicks the option for eg "x" and then they hit the clear button it will go back to "+".

    Heres my code,

     
    <html> 
    <head> 
    <title>Web Arithmetic</title> 
    <style type="text/css"> 
    body{
          background-color: green;
          color: gold;
          font-size: 20px;
          text-align: center;
        }
    </style> 
    <script type="text/javascript"> 
    function validate()
    {
    document.frmArithmetic.submit();
    }
     
    function clean()
        {
          document.frmArithmetic.txtNum1.value= "";
          document.frmArithmetic.txtNum2.value= "";
          document.frmArithmetic.txtNum1.focus();
        }
    </script> 
    </head> 
    <body onload="document.frmArithmetic.txtNum1.focus()"> 
     
    <h1>Web Arithmetic</h1> 
    <br /><br /><br /><br /><br /> 
    <form name="frmArithmetic" action="jpdset1_1.php"> 
    First number: <input type="text" name="txtNum1" /> 
    <br /><br /><br /><br /> 
    Second number: <input type="text" name="txtNum2" /> 
    <br /><br /><br /><br /> 
     
    Choose an operator: <br /> 
    <input type="radio" name="optOp" value="+" checked /> +
    &nbsp; &nbsp; &nbsp;
    <input type="radio" name="optOp" value="-" /> -
    &nbsp; &nbsp; &nbsp;
    <input type="radio" name="optOp" value="x" /> x
    &nbsp; &nbsp; &nbsp;
     
    <input type="radio" name="optOp" value="/" /> /
    &nbsp; &nbsp; &nbsp;
    <input type="radio" name="optOp" value="%" /> %
    <br /><br /><br /> 
    <input type="button" value="Calculate" name="cmdCalculate" onclick="validate()" /> 
    &nbsp; &nbsp; &nbsp;
    <input type="button" value="Clear" name="cmdClear" onclick="clean()" /> 
     
    </form> 
    </body> 
    </html>
    
    Code (markup):
    I'm not sure how to do it so if anyone can help me out that would be great. Cheers.:)
     
    Luke Watson, Feb 28, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Add this to clean function:
    document.frmArithmetic.optOp[0].checked = true;
     
    Cash Nebula, Mar 1, 2011 IP
  3. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #3
    Try:

    
    <input type="reset" name="Reset" id="button" value="Reset" />
    
    Code (markup):
    This button will reset your all input fields and u can call function for focus onclick.

    
    <input type="reset" name="Reset" id="button" value="Reset" onClick="document.frmArithmetic.txtNum1.focus()"; />
    
    Code (markup):
     
    HungryMinds, Mar 3, 2011 IP