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

  • Home
  • SEARCH
  • 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 4065616
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:00:22+00:00 2026-05-20T16:00:22+00:00

I am new to Javascript so please bear with me. I have a Javascript

  • 0

I am new to Javascript so please bear with me. I have a Javascript quiz which collects the answers and displays a score at the end of the quiz. The score is calculated on the event of a ‘onClick’command at the end of the form, which I would then like to parse through to event tracking to Google analytics. At the moment, when the button is clicked if I try and parse the value of the score to the analytics it will not work. The exact same method does work if the variable is another defined integer (with a fixed value).

Here is my code:

var numQues = 5;    
var numChoi = 3;   
var url = location.href;  
var score = 0;  
var answers = new Array(5);  
answers[0] = "Play your pet a CD";  
answers[1] = "National PetLog database";  
answers[2] = "Certain types of cancers";  
answers[3] = "A few weeks";  
answers[4] = "Up to 1 year";  

function getScore(form) {  
 score = 0;  
  var currElt;  
  var currSelection;  

  for (i=0; i<numQues; i++) {  
    currElt = i*numChoi;  
    for (j=0; j<numChoi; j++) {  
      currSelection = form.elements[currElt + j];  
      if (currSelection.checked) {  
        if (currSelection.value == answers[i]) {  
          score++;  
          break;  
        }  
      }  
    }  
  }  
  form.score.value = score + "/" + numQues;  
  var correctAnswers = "";  
  for (i=1; i<=numQues; i++) {  
    correctAnswers += i + ". " + answers[i-1] + "\r\n";  
  }  
  form.solutions.value = correctAnswers;  


}  

function JavaScriptFunction(){  
  return(score);  
}

and the button is:

<input class="ScoreButton" onclick="getScore(this.form); pageTracker._trackEvent('Quiz', 'Petcare quiz', url, score);" type="button" value="Get score" />

Any help will be much appreciated.

  • 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-20T16:00:22+00:00Added an answer on May 20, 2026 at 4:00 pm

    You are not sending the value to the tracker, try this:

    <input class="ScoreButton" onclick="doStuff(this.form);" type="button" value="Get score" />
    

    Edit:

    function getScore(form) {  
     score = 0;  
      var currElt;  
      var currSelection;  
    
      for (i=0; i<numQues; i++) {  
        currElt = i*numChoi;  
        for (j=0; j<numChoi; j++) {  
          currSelection = form.elements[currElt + j];  
          if (currSelection.checked && currSelection.value == answers[i]) {  
              score++;  
              break;  
          }  
        }  
      }
    
      return score;  
    }
    
    function doStuff(form){
       //calculate score
       score = getScore(form);
    
       //replace value
       form.score.value = score + "/" + numQues;  
    
       //correct answers
       var correctAnswers = "";  
       for (i=1; i<=numQues; i++) {  
         correctAnswers += i + ". " + answers[i-1] + "\r\n";  
       }  
       form.solutions.value = correctAnswers;  
    
       //send data to track
       pageTracker._trackEvent('Quiz', 'Petcare quiz', url, score);
    }
    

    Edit: I’ve refactored a little bit your code. Maybe this way works or can find where is the error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to JavaScript OO programming please bear with me for any ambiguities.
I'm new to JQuery, so please bear with me =) I have a Menu
Wondering how to open many new windows with Javascript. I have found plenty of
I'm kind of new to this so please bear with me... Let's say I
I am new to JavaScript OOP. Can you please explain the difference between the
I am new to Javascript, JSON and jQuery. So please be easy on me.
I am new to HighCharts, javascript, and rails. I have a highchart in my
I'm extremely new to mvc and webtest so please bear wtih me here. I
I am relatively new to javascript so please be patient if what i am
I'm quite new to javascript, but have managed to write a working xml function

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.