Working JS Fiddle of the below: https://jsfiddle.net/b63t295x/2/
I have a series of divs, each consisting of a question, an input field, and a text link that acts as a button that when clicked, will toggle the currently showing div and replace it with the next in the line:
<head>
<script type="text/javascript">
function displayquestion(a){
var questions = document.getElementsByClassName("questionholder");
for(var i=0; i < questions.length; i++) {
questions[i].style.display = "none";
}
var nextQuestion = document.getElementById("question" + a);
if(nextQuestion !== null) {
nextQuestion.style.display = "block"
}
}
</script>
</head>
<div id="requiredMessage" style="display:none">This field is required.
</div>
<form id="TheForm" style="display:block;">
<div class="questionholder" id="question0" style="display:block">
<a class="text2button" onclick="displayquestion(1)">Start</a>
</div>
<div class="questionholder" id="question1" style="display:none">
<h5>Surname</h5>
<input id="required" name="ln"><br>
<a class="text2button" onclick="displayquestion(2)">Next</a>
</div>
<div class="questionholder" id="question2" style="display:none">
<h5>Given Name</h5>
<input name="gn"><br>
<a class="text2button" onclick="displayquestion(3)">Next</a>
</div>
.... and so on 35 times
</form>
HOWEVER, I want the Next button to only function IF the input field is filled in (contents doesn't matter, just not blank).
How can this be achieved without jQuery?
UPDATE
JSFIDDLE for the following: I have gotten so far as this which will display an error message if the input is blank. However, it if I type in something, it won't move on to the next question.
function displayquestion(a){
var currentDIV = document.getElementById("question" + a);
var currentInput = document.querySelector('input').value;
var questions = document.getElementsByClassName("questionholder");
var showRequired = document.getElementById("requiredMessage");
console.log(currentInput == '');
if (a == 1){ // Enter here all question # that should be IGNORED. question0 = 1, question1 = 2, etc
showRequired.style.display = "none";
for(var i=0; i < questions.length; i++) {
questions[i].style.display = "none";
}
var nextQuestion = document.getElementById("question" + a);
if(nextQuestion !== null) {
nextQuestion.style.display = "block";
}
} else {
if (currentInput == '') {
showRequired.style.display = "block";
}
}
}