944,129 Members | Top Members by Rank

Ad:
Jul 18th, 2005
0

setting value is not sticking

Expand Post »
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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function display(number)
  2. {
  3. var x = document.form1.displayText;
  4. x.value = number;
  5. }

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.
Similar Threads
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 19th, 2005
0

Re: setting value is not sticking

It's impossible to tell with only the code you showed. Nothing in those two lines would cause the text to disappear.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jul 19th, 2005
0

Re: setting value is not sticking

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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html><head>
  2. <title>JavaScript Calculator</title>
  3. <script language="JavaScript" type="text/javascript">
  4. function display(number)
  5. {
  6. var x = document.form1.displayText;
  7. x.value = number;
  8. }
  9. </script>
  10. </head>
  11. <body>
  12. <form name="form1">
  13. <table>
  14. <tr>
  15. <input type="text" name="displayText" size="30">
  16. </tr>
  17. <tr>
  18. <td><input type="submit" value="7" name="button7"
  19. onClick="display('7')"></td>
  20. <td><input type="submit" value="8" name="button8"
  21. onClick="display(8)"></td>
  22. <td><input type="submit" value="9" name="button9"
  23. onClick="display(9)"></td>
  24. <td><input type="submit" value="/" name="buttonDivide"></td>
  25. <td><input type="submit" value="sqrt" name="buttonRoot"></td>
  26. </tr>
  27. <tr>
  28. <td><input type="submit" value="4" name="button4"
  29. onClick="display(4)"></td>
  30. <td><input type="submit" value="5" name="button5"
  31. onClick="display(5)"></td>
  32. <td><input type="submit" value="6" name="button6"
  33. onClick="display(6)"></td>
  34. <td><input type="submit" value="*" name="buttonMultiply"></td>
  35. <td><input type="submit" value="^2" name="buttonSquare"></td>
  36. </tr>
  37. <tr>
  38. <td><input type="submit" value="1" name="button1"
  39. onClick="display(1)"></td>
  40. <td><input type="submit" value="2" name="button2"
  41. onClick="display(2)"></td>
  42. <td><input type="submit" value="3" name="button3"
  43. onClick="display(3)"></td>
  44. <td><input type="submit" value="-" name="buttonMinus"></td>
  45. <td><input type="submit" value="1/x" name="buttonOneX"></td>
  46. </tr>
  47. <tr>
  48. <td><input type="submit" value="0" name="button0"
  49. onClick="display(0)"></td>
  50. <td><input type="submit" value="." name="buttonPeriod"></td>
  51. <td><input type="submit" value="+/-" name="buttonPlusOrMinus"></td>
  52. <td><input type="submit" value="+" name="buttonPlus"></td>
  53. <td><input type="submit" value="=" name="buttonEquals"></td>
  54. </tr>
  55. </table>
  56. </form>
  57. </body>
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 19th, 2005
0

Re: setting value is not sticking

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".
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jul 19th, 2005
0

Re: setting value is not sticking

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

Thanks for helping.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Problem using onload
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Please i need some help with js





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC