I'm making a guessing game project and I have all the logic completed I am just having trouble with the output. The user enters their name and guess and submits and the program will check to see if the guess is correct,too high, too low and output their name + (Too high etc.) right below where you enter your guess. How I have mine set up, when you click the submit button it gets rid of all the text boxes and just outputs if you are right or wrong. I know I need to reinvoke the function to obtain this output, but I am rather new to JS and I'm not quite sure how to achieve this. Any tips and pointers are appreciated thank you all!
CODE:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Number Guess My Number Game!</title>
<style>
html {
font-family: sans-serif;
}
body {
width: 50%;
max-width: 800px;
min-width: 480px;
margin: 0 auto;
}
</style>
</head>
<body>
<h1>Guess My Number!</h1>
<p>Valid Numbers are: 1 through 100</p>
<div class="form">
<label for="nameField">Enter your first NAME: </label>
<input type="text" id="nameField" class="nameField"><br>
<label for="submit"> Enter your Guess: </label>
<input type="text" id="guessField" class="glassField">
<input type="submit" value="Submit data" class="submit" id="submit">
</div>
<script type="text/javascript">
var y = Math.floor(Math.random() * 100) + 1;
document.getElementById("submit").onclick = function() {
var x = document.getElementById("guessField").value;
if (x == y) {
document.write(nameField + "You are right! Enter a new number to try
again!");
}
else if (x > y) {
document.write(nameField + "Your guess is too high");
} else {
document.write(nameField + "Your guess is too low");
}
}
</script>
</body>
</html>