How to get selected star value in jQuery Raty

I am using jQuery Raty Plugin which is a star rating plugin and it works fine.

Here is my code

 $('#star').raty({
            path: '/Content/RatingPlugin/',
            numberMax: 5,
            score: function () {
                return $(this).attr('data-score');
            }
        });

      

Html

<div id="star" data-score="@Model.Company.Rating"></div>

      

Now there are 2 problems, suppose when my page load @Model.Company.Rating

is equal 4

it will show 4 stars on which it is correct, but if someone clicks on the third star it will disconnect from that 4th star and then only 3 stars will be included. I want that if the user clicks on any star and then in javascript, I get the value of that star and store that in the database, but that should not affect the user. I mean after clicking the custom quantity on stars

will remain the same.

If you need any other part, please let me know.

+3


source to share


2 answers


http://jsfiddle.net/DerekL/bp2mW/18/

$('#star').raty({
    score: 3,                                                    //default score
    starOn: "http://wbotelhos.com/raty/lib/img/star-on.png",
    starOff: "http://wbotelhos.com/raty/lib/img/star-off.png",
    readOnly: true                                               //read only
});

$("#star > img").click(function(){
    var score = $(this).attr("alt");                             //record clicked
    alert(score);                                                // value of the
    //save to database                                              star
});

      



Read more on readonly

in plugins .

+4


source


After initializing the object, you can call .raty('score')

to get the evaluation value. The value will be undefined if not set.

var currentScore = $('#star').raty('score');

      



Documentation: http://wbotelhos.com/raty#functions

+6


source







All Articles