-2

My code

I am trying to clear fields after submit. I have tried everything I seen on google search nothing is working

<input type="text" id="a1">
  <input type="button" class="button" value="Submit" onclick="w001()">
    <span id="a21"></span>
</form>

function w001() {
  var a = document.getElementById("1");
  if ((a.value == "a 7") || (a.value == "7")) {
    document.getElementById('a21').innerHTML = 'correct';
  } else {
    document.getElementById('a21').innerHTML = 'incorrect';
  }
`function w001Field() {
  if (document.getElementById) {
    document.a1.value = "";
  }
}
`}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Kaz Tee
  • 3
  • 2

2 Answers2

1

document.a1.value = ""; is not valid.

use document.getElementById("a21").value = '' or

Just a.value = '' because you've defined your input element on var a = document.getElementById("a21");

Final code

function w001() {
    var a = document.getElementById("a21");
    if ((a.value == "a 7") || (a.value == "7")) {
        document.getElementById('answera21').innerHTML = 'correct';
    } else {
        document.getElementById('answera21').innerHTML = 'incorrect';
    }
    a.value = '' // reset value of input with id a21
}
Danu Akbar
  • 196
  • 1
  • 2
  • 14
0

this id:

var a = document.getElementById("1");

does not exist

it should be

var a = document.getElementById("a1");

this function;

w001Field()

Is never used

Change the code to

<form onsubmit="return false">
    <input type="text" id="a1">
    <input type="button" class="button" value="Submit" onclick="w001()">
    <span id="a21"></span>
</form>

    function w001() {
    var a = document.getElementById("a1");

    document.getElementById('a21').innerHTML = ((a.value == "a 7") || (a.value == "7")) ? 'correct' : 'incorrect';

    document.getElementById("a1").value = "";
}
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Star089
  • 47
  • 2
  • 11