I'm encountering an issue with my function in this JSFiddle
window.addEventListener("load", init, false);
function init() {
let name = document.getElementById("name").addEventListener("blur", checkName, false);
let startButton = document.getElementsByTagName("button")[0].addEventListener("click", startClicked, false);
}
function checkName(event) {
const regEX = /^\d[A-Z]{2}\d{1,}K$/;
let name = document.getElementById("name");
let button = document.getElementsByTagName("button")[0];
if (regEX.test(name.value)) {
button.removeAttribute("disabled");
button.setAttribute("enable");
}
}
function startClicked(event) {
let button2 = document.getElementById("choice1");
button2.removeAttribute("disabled");
let button3 = document.getElementById("choice2");
button3.removeAttribute("disabled");
showQuestion();
}
function showQuestion(event) {
let question = document.getElementById('question');
question.innerHTML = "Hello";
}
body {
font-family: sans-serif;
font-size: large;
}
form {
text-align: center;
}
button {
width: 300px;
height: 100px;
font-size: large;
}
form div {
margin: 2em;
}
#report {
display: none;
position: absolute;
top: 100px;
left: 100px;
background-color: lightgray;
opacity: 0.9;
padding: 3em;
box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<title>Exam p5>
<script src="scripts/exam.js.pdf.js"></script>
<script src="scripts/hands-off.js"></script>
<link rel="stylesheet" href="style/style.css">
</head>
<body>
<form>
<div>
<h1>Survey Tool</h1>
<p>First enter a correct code: start with a number, followed by 2 capital letters, then 1 or more numbers and the code ends with a K. Press start to begin the survey...</p>
<label for="name">Code: </label><input type="text" id="name" />
</div>
<button disabled>Start</button>
<div>
<p id="question">A question</p>
<button id="choice1" disabled>Choice1</button>
<button id="choice2" disabled>Choice2</button>
</div>
</form>
<section id="report">
<h1>Answers Overview</h1>
<table>
</table>
</section>
</body>
</html>
The required code input is
1KK1K
The problem arises when the listener handler is triggered:
function startClicked(event) {
let button2 = document.getElementById("choice1");
button2.removeAttribute("disabled");
let button3 = document.getElementById("choice2");
button3.removeAttribute("disabled");
showQuestion();
}
It causes an automatic page refresh back to the default state.
Note: I am unable to directly modify the HTML, all changes must be made through JavaScript.