Question

i'm using jquery raty with 5 rating options on a page. I want to tally up the score for all 5 ratings. i've given each rating its own ID #targetKeep1 #targetKeep2 etc... and changed the scoreName to be unique. i would like to somehow add up all the scores and display it. How would I go about doing this?

would it be something like function(score, evt) { var answer1 = score; var answer2 = score; var answer3 = score; etc.. } ?

<script type="text/javascript">
    $(function() {
      $.fn.raty.defaults.path = 'img/';
      $('#targetKeep1').raty({
        cancel      : false,
        width       : 310, 
        number      : 6,
        target      : '#targetKeep1-hint',
        single      : true,
        hints       : ['0', '1', '2', '3', '4' , '5'],
        targetKeep  : true,
        scoreName   : 'answer1',
          click: function(score, evt) {
    alert('ID: ' + $(this).attr('id') + "\nscore: " + score + "\nevent: " + evt);
  }
      });
    });
    $(function() {
      $.fn.raty.defaults.path = 'img/';
      $('#targetKeep2').raty({
        cancel      : false,
        width       : 310, 
        number      : 6,
        target      : '#targetKeep2-hint',
        single      : true,
        hints       : ['0', '1', '2', '3', '4' , '5'],
        targetKeep  : true,
        scoreName   : 'answer2',
      });
    });
Was it helpful?

Solution

I would not bother with a unique scoreName unless you needed it for another reason.

I would assign a class (e.g. targetKeep) to the relevant divs for which you have called raty.

Then you could do the following, with the standard score field:

var totalScore = 0;
$(".targetKeep").each( function(tK) {
   if (typeof($(tK).raty('score')) != "undefined") {
      totalScore += $(tK).raty('score');
   }
});

Fiddle here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top