Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
XML
<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
function calculate()
{
var x=eval(document.getElementById("txt_area").value);
document.getElementById("txt_area").value = x;
}
function display(clicked_id)
{
    var pre = document.getElementById("txt_area").value;

    pre= pre + document.getElementById(clicked_id).value;

    document.getElementById("txt_area").value=pre;

}
</script>
</head>
<body>
         <form action="" name="myForm">
                 <table border="1">
                     <tr>
                       <td align="right"><input type="text name="text" id="txt_area"/></td>
                     </tr>
                     <tr>
                       <td align="right"> <input type="button" onclick="display(this.id)" id="1" value="1"/><input name="2" onclick="display(this.id)" id="2" type="button" value="2"/><input name="3" onclick="display(this.id)" id="3" type="button" value="3"/><input onclick="display(this.id)" id="+" type="button" value="+"/></td>
                     </tr>
                     <tr>
                       <td align="right"><input type="button" id="4" onclick="display(this.id)" value="4"/><input type="button" onclick="display(this.id)" id="5" name="5" value="5"/><input name="6" onclick="display(this.id)" id="6" type="button" value="6"/><input onclick="display(this.id)" id="-" type="button" value="-"/></td>
                     </tr>
                     <tr>
                        <td align="right"><input type="button" onclick="display(this.id)" id="7" value="7"/><input type="button" onclick="display(this.id)" id="8" value="8"/><input onclick="display(this.id)" id="9" type="button" value="9"/><input onclick="display(this.id)" id="*" type="button" value="*"/></td>
                     </tr>
                     <tr>
                         <td align="right"><input type="submit" value="Submit" onClick="calculate()"/></td>
                     </tr>
                </table>

         </form>

 </body>
 </html>
Posted

Hey there,

You need to prevent the Button's PostBack after it has calculated and set the answer, do this:

Replace your Submit button with this:
HTML
<input type="submit" value="Submit"  önclick="return calculate();" />

and add a return false at the end of calculate() function:
JavaScript
function calculate() {
            var x = eval(document.getElementById("txt_area").value);
            document.getElementById("txt_area").value = x;
            return false;
        }


Let me know if it helps.

Azee...
 
Share this answer
 
v3
just chnaged the buttons type from SUBMIT to BUTTON. that helped too.

Thanks "Azee".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900