Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
|
I've been experimenting a little with your code, and I found that the following works:
Code:
<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JS Calculator</title>
<script language="JavaScript">
/* This Function places the value of the number button into the display box */
function btnNumber(whichNumber)
{
document.getElementById('Result').value=(document.getElementById('Result').value + whichNumber);
}
/* This function tells the script which calculation symbol has been chosen and resets the number display box */
function btnMethod(whichMethod)
{
whichfirstValue = document.getElementById('Result').value;
methodValue = (whichMethod);
document.getElementById('Result').value = ("");
}
/* This function carries out the sum based on an If deciding how to calculate based on which calculation button was pressed */
function btnResult()
{
whichValue = document.getElementById('Result').value;
switch(methodValue)
{
case "1":
{whichResult = (parseInt(whichfirstValue) + parseInt(whichValue));
alert(whichResult);}
break;
case "2":
{whichResult = (whichfirstValue - whichValue);
alert(whichResult);}
break;
case "3":
{whichResult = (whichfirstValue * whichValue);
alert(whichResult);}
break;
case "4":
{whichResult = (whichfirstValue / whichValue);
alert(whichResult);}
break;
default:
alert("You must enter some numbers!");
}
}
/* Reset button */
function btnClear()
{
document.getElementById('Result').value = ("");
}
</script>
</head>
<body>
<textarea rows="1" cols="6" id="Result">
</textarea>
<br>
<br>
<button type="button" OnClick="btnMethod('1')">+</button>
<button type="button" OnClick="btnMethod('2')">-</button>
<button type="button" OnClick="btnMethod('3')">x</button>
<button type="button" OnClick="btnMethod('4')">/</button>
<button type="button" OnClick="btnResult()">=</button>
<br>
<br>
<button type="button" OnClick="btnNumber('1')">1</button>
<button type="button" OnClick="btnNumber('2')">2</button>
<button type="button" OnClick="btnNumber('3')">3</button>
<br>
<button type="button" OnClick="btnNumber('4')">4</button>
<button type="button" OnClick="btnNumber('5')">5</button>
<button type="button" OnClick="btnNumber('6')">6</button>
<br>
<button type="button" OnClick="btnNumber('7')">7</button>
<button type="button" OnClick="btnNumber('8')">8</button>
<button type="button" OnClick="btnNumber('9')">9</button>
<button type="button" OnClick="btnNumber('0')">0</button>
<br>
<br>
<button type="button" OnClick="btnClear()">CLEAR</button>
</body>
</html>
Note: As you might notice, I've replaced Result.value with document.getElementById('Result').value and the textarea has now id="Result" instead of name="Result".
I did this because otherwise it will only work in Internet Explorer.
document.getElementById(''); is a much more supported function.
Note 2: Apparently, you don't have to remove the curly brackets. I didn't really expect that. 
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
|