2

I am doing some sort of dynamic quiz using jquery. I am currently stuck at hiding/showing the divs using the same button. So, for example, I have this in html:

<div id="div1" class="question">
<div id="div2" class="question">
<div id="div3" class="question">
<div id="div4" class="question">
<div id="div5" class="question">

<button type="button" onclick="GetSelectedItem()" id="next">Next</button>

This is the jquery part:

$(document).ready(function (){

$('#div2, #div3, #div4, #div5').hide();
$('#next').click(function(){
     //code
});
});

Using the '#next' button I would like to go through every div, so when I press it it will hide the previous div and show the next one and so on. Could anyone point me in the right direction? Thanks

6 Answers 6

3

You could try something like this (assuming all your questions have a class of question and nothing else does):

$('#next').click(function(){
    var curr = $(".question:visible");
    curr.next(".question").show();
    curr.hide();
});

See here

Note you can check the value returned by next() to handle the end of your quiz.

Here's an example that disables the next button when you get to the last question.

Sign up to request clarification or add additional context in comments.

1 Comment

Is amazing how much ways we can solve a problem using jQuery \o/
2

First off you had errors in your HTML, you weren't closing the divs

This should start you out:

http://jsfiddle.net/crZ69/

HTML

<div id="div1" class="question">q1</div>
<div id="div2" class="question">q2</div>
<div id="div3" class="question">q3</div>
<div id="div4" class="question">q4</div>
<div id="div5" class="question">q5</div>

<button type="button" onclick="GetSelectedItem()" id="next">Next</button>

JQUERY

var qCount = 1;

$('.question').hide();

$('#next').click(function(){
    $('.question').hide();
    $('#div' + qCount).show() ;   
    qCount = qCount + 1;

    if(qCount == 6){ qCount = 1;}
});

Comments

2

You can create an index to keep track of the open div and increment it when you click the button, hiding all the divs and then only showing the "next" one.

$('#div2, #div3, #div4, #div5').hide();
var idx = 0;
$('#next').click(function () {
    idx++;
    if (idx < $('.question').length) $('.question').hide();
    $('.question').eq(idx).show();
});

jsFiddle example

Comments

1

This uses a current CSS class to keep track of the current question for visibility purposes and tidies up the HTML a bit to use unobtrustive Javascript - http://jsfiddle.net/ZqTsv/

HTML:

<div id="div1" class="question current">Question 1</div>
<div id="div2" class="question">Question 2</div>
<div id="div3" class="question">Question 3</div>
<div id="div4" class="question">Question 4</div>
<div id="div5" class="question">Question 5</div>

<button type="button" id="next">Next</button>

CSS:

.question {
    display: none;
}

.question.current {
    display: block;
}

jQuery:

$("#next").click(function () {
    if ($(".current").next(".question").length) {
       $(".current").removeClass("current").next(".question").addClass("current");     
    }    
});

Comments

0

it's amazing how many different solutions different people come up with. here is mine:

First fix your html, so the divs have content.

This is the javascript I propose:

var idx = 0;
$(document).ready(function (){
    var qs = $('.question');
    qs.hide();
    qs.first().show();
    $('#next').click(function(){
       idx++;
       if( idx >= qs.length ) idx = 0;
       qs.hide();
       qs.eq(idx).show();
    });
});

Fiddle

Comments

0

I did something

I think it will be usefull to you =)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.