I'm trying to make a simple calculator and whenever I want to add text to the textfield(say a number) it will show up, then will dissapear.

function display(number)
			{
				var x = document.form1.displayText;
				x.value = number;
			}

This is how I'm doing it, but why is the text dissapearing right after it appears?
Note: This method is called whenever one of the number buttons is clicked.

Recommended Answers

All 4 Replies

It's impossible to tell with only the code you showed. Nothing in those two lines would cause the text to disappear.

I know, that's what I don't understand. It' almost like it's refreshing the page or something. Here's the code I've got. I know the thing looks really ugly but I'm a beginner at JavaScript:

<html><head>
		<title>JavaScript Calculator</title>	
		<script language="JavaScript" type="text/javascript">
			function display(number)
			{
				var x = document.form1.displayText;
				x.value = number;
			}
		</script>
      </head>
      <body>
        <form name="form1">
      	<table>
		<tr>
			<input type="text" name="displayText" size="30">
		</tr>
		<tr>
			<td><input type="submit" value="7" name="button7"
			     onClick="display('7')"></td>
			<td><input type="submit" value="8" name="button8"
			     onClick="display(8)"></td>
			<td><input type="submit" value="9" name="button9"
			     onClick="display(9)"></td>
			<td><input type="submit" value="/" name="buttonDivide"></td>
			<td><input type="submit" value="sqrt" name="buttonRoot"></td>
		</tr>
		<tr>
			<td><input type="submit" value="4" name="button4"
				onClick="display(4)"></td>
			<td><input type="submit" value="5" name="button5"
				onClick="display(5)"></td>
			<td><input type="submit" value="6" name="button6"
				onClick="display(6)"></td>
			<td><input type="submit" value="*" name="buttonMultiply"></td>
			<td><input type="submit" value="^2" name="buttonSquare"></td>
		</tr>
		<tr>
			<td><input type="submit" value="1" name="button1"
				onClick="display(1)"></td>
			<td><input type="submit" value="2" name="button2"
				onClick="display(2)"></td>
			<td><input type="submit" value="3" name="button3"
				onClick="display(3)"></td>
			<td><input type="submit" value="-" name="buttonMinus"></td>
			<td><input type="submit" value="1/x" name="buttonOneX"></td>
		</tr>
		<tr>
			<td><input type="submit" value="0" name="button0"
				onClick="display(0)"></td>
			<td><input type="submit" value="." name="buttonPeriod"></td>
			<td><input type="submit" value="+/-" name="buttonPlusOrMinus"></td>
			<td><input type="submit" value="+" name="buttonPlus"></td>
			<td><input type="submit" value="=" name="buttonEquals"></td>
		</tr>
	</table>
	</form>
     </body>

That's exactly what's happening, because of all your submit buttons. You are submitting the form... that's not what you want to do. Change all the "submit" to "button".

THANK YOU so much. I would have never spotted or known what was happening.

Thanks for helping.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.