# Clearing input text in calculator code

**URL:** <https://forum.kirupa.com/t/clearing-input-text-in-calculator-code/256877>\
**Category:** flash\
**Created:** [April 8, 2008, 11:50am UTC](https://forum.kirupa.com/t/clearing-input-text-in-calculator-code/256877 "2008-04-08T11:50:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![she\_sins](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@she\_sins](https://forum.kirupa.com/u/she_sins)\
**Post date:** [April 8, 2008, 11:50am UTC](https://forum.kirupa.com/t/clearing-input-text-in-calculator-code/256877/1 "2008-04-08T11:50:40Z")

</div>

I have been working on creating a flash calculator with actionscript 2

It is all working fine, except I cannot work out the code i need for the ‘CE’ or clear button. I want it to just clear the input fields ready for new data.

Any ideas?

I’m new here so don’t know if theres a way I can send you the FLA, but the script is:

[COLOR=Blue]/\* Calculator program, reads text from input (and validates text fields) and uses math class to display text at runtime  
\*/

//declaring variables  
var value1:Number; //Assign data type as number  
var value2:Number;  
var answer:Number;

add\_btn.onRelease = function ( ) //Release of button performs function  
{

```
readInputs();
if ( validateInput() ){ //validation to ensure numbers are entered
    performOperation( "add" );
    displayResult();
} else {
    output_txt.text = "Please enter numbers" // display message if non-numbers are entered
}

```

}

subtract\_btn.onRelease = function ( ) //Release of button performs function  
{  
readInputs();  
if ( validateInput() ){ //validation to ensure numbers are entered  
performOperation( “subtract” );  
displayResult();  
} else {  
output\_txt.text = “Please enter numbers” // display message if non-numbers are entered  
}

}

divide\_btn.onRelease = function ( ) //Release of button performs function  
{  
readInputs();  
if ( validateInput() ){ //validation to ensure numbers are entered  
performOperation( “divide” );  
displayResult();  
} else {  
output\_txt.text = “Please enter numbers” // display message if non-numbers are entered  
}

}  
multiply\_btn.onRelease = function ( ) //Release of button performs function  
{  
readInputs();  
if ( validateInput() ){ //validation to ensure numbers are entered  
performOperation( “multiply” );  
displayResult();  
} else {  
output\_txt.text = “Please enter numbers” // display message if non-numbers are entered  
}

}  
//attempt to add a clear function which clears data from input fields  
clear\_btn.onRelease = function ()  
{  
clear;  
}

function readInputs() { //function for reading inputs

```
value1 = Number(input1_txt.text); //value1 is read from input1_text
value2 = Number(input2_txt.text); 

```

}  
function validateInput(){ //vaslidation of input

```
if ( isNaN(value1) || isNaN(value2) ){
    return false;
} else {
    return true;
}

```

}

function performOperation( op:String ){ //performOperation function to do calculations

```
if ( op == "add") {
    answer = value1 + value2;
}
if ( op == "subtract") {
    answer = value1 - value2;
}
if ( op == "divide") {
    answer = value1 / value2;
}
if ( op == "multiply") {
    answer = value1 * value2;
}

```

}

function displayResult() { //Displaying the answer in the output dynamic text box  
output\_txt.text = answer;  
}  
[/COLOR]
