Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:26:39+00:00 2026-05-31T19:26:39+00:00

im trying to get my jquiz class to count for 2 IDs for each

  • 0

im trying to get my jquiz class to count for 2 IDs for each page.
It only counts the first page correctly. The second page just displays the same score as the first page. Im not sure what im doing wrong

This is what i have:

JQuery:

$(".jquiz li ul li").click(function()
{
    var count1 = 0; //page1 counter
    var count2 = 0; //page2 counter

//right answer
if ($(this).hasClass("correct")) 
{

    if ($("#page1"))
        count1++; //page1 
    if ($("#page2"))
        count2++; //page2
}

 //page1 quiz counter display   
if ($('ul.answered').length == 3) 
{
    $('#page1mark').fadeIn('slow');
    $('#page1total').html('You got a '+count1+' out of '+3+' on the page1 quiz.');
}

//page2 quiz counter display        
if ($('ul.answered').length == 6) 
{
    $('#page2mark').fadeIn('slow');
    $('#page2total').html('You got a '+count2+' out of '+3+' on the page2 quiz.');
}

HTML: Note: the class jquiz is in the tag element OL. It wouldnt let me post the code

   <id="page1" class="jquiz">
   pizza is yum?
   <Ii class ="correct"> true</Ii> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:26:40+00:00Added an answer on May 31, 2026 at 7:26 pm

    You’re going about it all wrong.

    You should either be doing this whole code block for each quiz, localizing all selectors to that dom element, or you can do this in one event handler, but inside it find out what quiz was activated at the top first (using jQuery.closest('.jquiz')), and limit everything else to within that dom node.

    Either way, it looks like each quiz has a different number of questions, so you should store the number of questions for each quiz in the dom, like this:

    <ol id="page1" class="jquiz" data-questions="3">
        ....
    </ol>
    
    <ol id="page2" class="jquiz" data-questions="6">
        ....
    </ol>
    

    Then, here’s an example of the javascript updated with the first method I described above — setting up a separate callback for each quiz (I think the first method produces more readable code, and the efficiency cost is negligible here). This also incorporates the other above change I suggested:

    // For each quiz...
    $('.jquiz').each(function() {
        // Cache the current quiz element, limit all selectors below to this element
        var $quiz = $(this);
    
        // This is a local variable, so it there will be a different copy for each quiz
        var correct_answers = 0;
    
        // Create a callback for *this quiz*
        $quiz.find('li ul li').click(function() {
            //right answer
            if ($(this).hasClass("correct")) correct_answers++;
    
            //counter display   
            if ($quiz.find('ul.answered').length == $quiz.data('questions')) {
                $('#'+$quiz.attr('id')+'mark').fadeIn('slow');
                $('#'+$quiz.attr('id')+'total').html('You got a '+correct_answers+' out of '+$quiz.data('questions')+' on the '+$quiz.attr('id')+' quiz.');
            }
        });
    });
    

    Also, while not directly related to your question, this is worth noting. Looks like someone can just keep clicking on a correct item to increase their correct count indefinitely. Maybe instead of explicitly counting, just let the implicit count do the work. This is probably better even if the rest of your code prevents the possibility for such an incident:

    // For each quiz...
    $('.jquiz').each(function() {
        // Cache the current quiz element, limit all selectors below to this element
        var $quiz = $(this);
    
        // Create a callback for *this quiz*
        $quiz.find('li ul li').click(function() {
            //counter display   
            if ($quiz.find('ul.answered').length == $quiz.data('questions')) {
                var correct_answers = $quiz.find('li ul li.correct').length;
                $('#'+$quiz.attr('id')+'mark').fadeIn('slow');
                $('#'+$quiz.attr('id')+'total').html('You got a '+correct_answers+' out of '+$quiz.data('questions')+' on the '+$quiz.attr('id')+' quiz.');
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying get the html within .event_recur. $(.entry).each(function(){ alert($(this).find(.event_recur).html()); }); <div class=entry> <p
http://lxr.linux.no/linux+v2.6.35/include/linux/preempt.h#L21 I am just trying get the linux source. I saw this preempt count
I'm trying get only the Saturdays and Sundays between two dates, but I don't
Ok, I just bought the new 27 inch iMac and I am trying get
Trying to get a value from a object isn't working out. class Asset <
Trying to get these list headings to line up correctly, but I can't figure
Trying to get a login script working, I kept getting the same login page
Trying to get the home page to display a 4 column grid for the
I'm trying get a subset of keys for each hash in an array. The
I am trying get type of property of my class by using of reflection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.