Javascript 2 functions for onClick

I am new to Javascript, so please bear with me. I have a Javascript poll that collects answers and shows the result at the end of the quiz. The score is calculated by the onClick team event at the end of the form, which I would then analyze to track events in Google analytics. The moment the button is pressed, if I try to analyze the value of the rating for analytics, it will not work. Exactly the same method works if the variable is another specific 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 button:

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

Any help would be much appreciated.

+3
source share
3 answers

, :

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

:

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);
}

: . , , .

0

, JavaScript, strong JavaScript, ​​ jQuery, YUI, Dojo, .. , DOM . JavaScript , JavaScript, DOM .

- , :

jQuery?

+2

Instead of using the onclick attribute, you should use event handlers.

<input id="score-button" type="button" value="Get Score">

Then in javascript ...

var scoreButton = document.getElementById('score-button');
scoreButton.addEventListener('click', myFuncOne(), false);
scoreButton.addEventListener('click', myFuncTwo(), false);

Of course, as mentioned in other sentences, you really should use a javascript library such as jQuery, which provides many tools to make your life easier.

In jQuery, the above code may be shorter ...

$('#score-button').click(function() {
  myFuncOne();
  myFuncTwo();
});
0
source

All Articles