0

I've read a bit and tried to figure this out but can't manage to get it to work!

The problem is that first I need to set var count = 0; and then count each time $('.next') is clicked so I can later use it at the bottom of the code and get the end variable progress to be correct.

What am I doing wrong here?

$(document).ready(function () {
 var count = 0;
 $('.next').click(function () {
 var counter = count++;
 $('.question-holder:visible').slideUp().closest('.question-holder').nextAll('.question-holder').eq(0).delay(500).slideDown();
 });
 $('.prev').click(function () {
 $('.question-holder:visible').slideUp().closest('.question-holder').prevAll('.question-holder').eq(0).delay(500).slideDown();
 });
 $('#end').on('click', function () {
 $('.question-holder').slideUp().closest('.question-holder');
 $('#end').remove();
 $('.next').remove();
 $('.prev').remove();
 $('#submit-holder').fadeIn();
 });
 var countQuestions = $('div.question-holder').length;
 var splitCount = 100 / countQuestions;
 var progress = splitCount * counter;
 $("div.bar").css("width", progress + "%");
});
David Eisenstat
65.7k7 gold badges66 silver badges128 bronze badges
asked Apr 30, 2013 at 16:10
2
  • what is the issue now ? Commented Apr 30, 2013 at 16:12
  • You increment count and you use counter. Don't use the counter variable, it's useless. Commented Apr 30, 2013 at 16:13

2 Answers 2

2

Try this:

var count = 0;
$('.next').click(function () {
 count++;

...

var countQuestions = $('div.question-holder').length;
var splitCount = 100 / countQuestions;
var progress = splitCount * count;

Each time the .click() event is triggered, your counter variable is being reinitialised, hence wiping any count you are trying to keep track of.

Just increment the count variable and it'll work nicely.

answered Apr 30, 2013 at 16:12
Sign up to request clarification or add additional context in comments.

2 Comments

Also, you'll need to use count below instead of counter
That question was just edited.. it was previously 'count' >.<
1

You can use count var like this -

var progress = splitCount * count;

No need for variable counter here var counter = count++;

You need count++ only in click handler

Edit :

working demo --> http://jsfiddle.net/mohammadAdil/p3PFG/2/

You need to calculate and update your progress bar inside click handler

answered Apr 30, 2013 at 16:14

1 Comment

You are a king! Thank you very very much! Really appreciate! :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.