1

I want the user to enter the correct sum and verify that. On completion of verification it will bring a text file.

<fieldset>
    <legend style="font-size: 30px; color: red;">Verification table</legend>
    <form>
        <label>The sum of 7 + 2 : </label> <input type="number" id="inputNumberBox" />
        <button type="submit" id="submitBtn" onsubmit="myFunction()">Hit me Baby!!
        </button>
    </form>
</fieldset>
function myFunction() {
    var inputValue = document.getElementById("inputNumberBox").value;

    function checkValidation() {
        if (inputValue == 9) {
            var client = new XMLHttpRequest();
            client.open('GET', '/testText.txt');
            client.onreadystatechange = function() {
                alert(client.responseText);
            }
            client.send();
        } else {
            window.alert("Try again");
        }
    }
    clickButton.addEventListener("click", checkValidation, false);
}
Swift
  • 3,250
  • 1
  • 19
  • 35
Eva Stuart
  • 31
  • 4

1 Answers1

0

you have a lot of unnecessary functions which are not even called, you can remove your checkValidation() and readText() and the code inside them can be present as they are, see below what I mean -

function myFunction() {
  var inputValue = document.getElementById("inputNumberBox").value;
  var clickButton = document.getElementById("submitBtn");
  if (inputValue == 9) {
    // enter your code for getting the text file
  } else {
    window.alert("Try again");
  }
}

also in your html, you have already defined the event listener, onsubmit myFunction() is called, so the last line of you JS code is unnecessary again.
See the working example here.

Rohit Kumar
  • 2,048
  • 1
  • 14
  • 28