2

I am using jquery raty plugin, there is a click event which I am using to send rating data and then return new rating from database, but I cant figure out how to update returned rating, my code:

    $('.starrating').raty({
            number:10,
            score: function() {
                  return $(this).attr('data-rating');
                 },
            click: function(score, evt) {
                var contentid = $(this).attr('id');
                $.post('/main/rating.php',{score:score, contentid:contentid },
                        function(data){
                            //alert(data+' Score = '+score);
                            $(this).data('rating',2);
                });
            }
            });

I tried with below but no success;

$(this).data('rating',2);

Thanks for any help.

user969068
  • 2,818
  • 5
  • 33
  • 64
  • By setting `$.fn.data` you only setting jquery data cache linked to element. Not even real data attribute – ant_Ti Mar 26 '13 at 13:58

3 Answers3

2

Try $(this).raty({ score: 2 }); according to raty docs

P.S. if you additionaly need to set data attribute you can try this: $(this).raty({ score: 2 }).attr('data-rating', 2);

P.P.S. Little click event update for right handling multiple elements

$('.starrating').raty({
    number:10,
    score: function() {
        return $(this).attr('data-rating');
    },
    click: function(score, evt) {
        var target = $(this),
            contentid = target.attr('id');

        $.post('/main/rating.php',{score:score, contentid:contentid },
            function(data){
                target
                    .raty({
                        score: data.score
                    })
                    .attr('data-rating', data.score);
            });
        }
    });
ant_Ti
  • 2,385
  • 16
  • 14
  • I already have that, I have to update rating value dynamically. – user969068 Mar 26 '13 at 15:22
  • Well Thank you It works but not with $(this) but with element name $('.starrating'), and I have to re-add all params in .raty({params}) in callabck, its not very efficient way but since there is no other way I can find so thanks marking it solved. – user969068 Mar 26 '13 at 16:03
1

Had some trouble with this, this worked for me:

$('.rating').raty('score', score);
mtosic
  • 156
  • 1
  • 4
0

Based on your comments about your parameters being reset, I've added some ideas below.

   //SET RATY DEFAULTS  
        $.fn.raty.defaults.path = 'img';
        $.fn.raty.defaults.cancel = true;
        //etc

In your success function, reset the rating with the new data. Only options specified will be overridden.

$(this).raty('set', { option: value });

For example, update score

$(this).raty('set', { score: 2 });

If you are having trouble with this, try the code below (based on this answer Passing variables through to AJAX)

$.fn.raty.defaults.click= function(score, evt) 
{   
    var target = $(this),
    contentid = target.attr('id');
    postvalue(score,target, contentid);
}

function postvalue (score,target, contentid)
{
    $.post('/main/rating.php',{score:score, contentid:contentid },
    function(data)
    {
         target.raty({score: data.score}).attr('data-rating', data.score);
    });
}

NOTE: None of this code has been tested.

Community
  • 1
  • 1
TryHarder
  • 2,704
  • 8
  • 47
  • 65