0
I’m trying to make a quiz and I want it to appear one question at a time. I used the css property display: none;
to hide, and created a javascript function that takes as parameter the id of the div that has to be shown. By clicking on button
Start is shown the first question. Then I call the function again by clicking on button
Submit to show next question, but it does not appear.
Ps: I want to use javascript only
function show_question (n) {
//Esconde o button start
document.getElementById('start').style.display = 'none';
//Mostrar a pergunta
document.getElementById(n).style.display = 'inline';
}
.question_form {
display: none;
}
<main>
<button id="start" onclick="show_question('q1')">Start</button>
<div class="question_form" id="q1">
<form id="question1">
<h3>1. Which tag should used to represent the "header" of a document?</h3>
<ul>
<li><input type="radio" name="q1" value="a">head</li>
<li><input type="radio" name="q1" value="b">header</li>
<li><input type="radio" name="q1" value="c">heading</li>
<li><input type="radio" name="q1" value="d">main</li>
</ul>
<button type="submit" id="btn-salvar" onclick="show_question('q2')">Submit</button>
</form>
</div>
<div class="question_form" id="q2">
<form id="quetion2">
<h3>2. Which the following is a NOT feature of HTML?<h3>
<ul>
<li><input type="radio" name="q2" value="a">New Media Elements</li>
<li><input type="radio" name="q2" value="b">Form Input Types</li>
<li><input type="radio" name="q2" value="c">Local Storage</li>
<li><input type="radio" name="q2" value="d">Cookies</li>
</ul>
<button id="submit" onclick="show_question('q3')">Submit</button>
</form>
</div>
<div class="question_form" id="q3">
<form id="question3">
<h3>3. Which element is the most appropriate to wrap around each blog post on a page?</h3>
<ul>
<li><input type="radio" name="q3" value="a">section</li>
<li><input type="radio" name="q3" value="b">post</li>
<li><input type="radio" name="q3" value="c">article</li>
<li><input type="radio" name="q3" value="d">main</li>
</ul>
<button id="submit">Submit</button>
</form>
</div>
And as I do to hide the previous div?
– Diego Soares
vc only need to hide all other Ivs with the question_form class before displaying question. I updated the answer.
– Francinaldo Portela