Search code examples
jqueryratingraty

Is it possible to know if a star is on ? (JQuery Raty Plugin)


First of all, excuse-me if I do some english mistakes, I am french. I am using the raty jquery plugin in order to put something in my favorites, I have got 1 star which can be clicked to set something in my favorites. As I said in the title, I need to know if that star is on or not.

I already tried to use the score value but it returns the score of the star under the mouse (always 1) and not the actual selected score (undefined or 1 in my case).

Here is my code:

$('.fav-star').raty({
    mouseover: function(score,evt) {
        if(1){ //is raty on ?
            $(this).raty('set', { hints: ['Retire from favorites']});
        }
        else {
            $(this).raty('set', { hints: ['Add to favorites']});
        }
    }
}

I just need to know what is the right condition to put in the if(). Thanks for your help !


Solution

  • I think I solved the problem. I am working with laravel so I just put an id to each star with $loop->iteration and then accessed to the score with $('#fav-star{{$loop->iteration}} > input').val(). It looks like this now.

    $('#fav-star{{$loop->iteration}}').raty({
       //some code
       mouseover: function(score,evt) {
            if($('#fav-star{{$loop->iteration}} > input').val() == 1){
                $('#fav-star{{$loop->iteration}}').raty('set', { hints: ['Retire from favorites']});
            }
            else {
                $('#fav-star{{$loop->iteration}}').raty('set', { hints: ['Add to favorites']});
            }
        }
    });
    

    Thanks for your help.