Trevader24135
Trevader24135

Reputation: 77

how to reset the value of a textbox

I'm making just a simple math learning helper to help me learn javascript. I want to refresh The textbox after a wrong answer(the page refreshes after a right answer to get a new question), but I want the question to be the same if you get it wrong. Here's my code:

<!doctype html>
  <body>
    <center>
      <font size="5">
        <form Id="Input">
          <input type="text" name="Input">
          <input type="button" value="submit" ID="Button">
        </form>

    <script type="text/javascript">
      Button.addEventListener("click", Answer);
      var A = Math.floor(Math.random()*11);
      var B = Math.floor(Math.random()*11);
      var C = A +B;
      var Input = document.getElementById('Input');
      document.write(A + "+" + B + "=");

      function Answer() {
        if(Input.Input.value == C) {
          alert("correct!");
          window.location.reload();
        } else {
          alert("incorrect!");
          document.getElementById('txtField').value='new value here'
        }
      }
    </script>
  </body>
</html>

Upvotes: 0

Views: 418

Answers (2)

pizzarob
pizzarob

Reputation: 12029

You forgot to add an ID to your text box.

<!doctype html>
<body>
<center>
<font size="5">
<form Id="Input">
<input id="txtField" type="text" name="Input">
<input type="button" value="submit" ID="Button">
</form>
<script type="text/javascript">
Button.addEventListener("click", Answer);
var A = Math.floor(Math.random()*11);
var B = Math.floor(Math.random()*11);
var C = A +B;
var Input = document.getElementById('Input');
document.write(A + "+" + B + "=");
function Answer()
{
if(Input.Input.value == C)
{
alert("correct!");
window.location.reload();
}
else
{
alert("incorrect!");
document.getElementById('txtField').value='new value here'
}
}
</script>
</body>
</html>

Upvotes: 0

clancer
clancer

Reputation: 613

You just need to give the txtField id to the text input here is a working fiddle http://jsfiddle.net/eFf27/

<input id="txtField" type="text" name="Input">

Upvotes: 1

Related Questions