Search code examples
phpjqueryraty

Jquery Raty start plugin : set score for multiple div


I am using jquery star rating plugin "Raty" https://github.com/wbotelhos/raty. i am generating a result set from database with PHP. that includes score also. I want set score property for every individual rating component. how can I do that? I am using this syntax for showing the stars.

$('.stars_small').raty({
      readOnly : true,
      half  : true,
      score : 2,
      space : false
 });  

<div class="stars_small"></div>

there are many div in a single page with the class "stars_small" generated dynamically. I want to set "score" for every div.


Solution

  • $('.stars_small').raty({
                 readOnly : true,
                 half  : true,
                 score: function() {
                          return $(this).attr('data-rating');
                         }
                  space : false
             });  
    

    this worked fine for me. the plugin is adding a hidden textbox in every div with class="stars_small" like this

    <div class="stars_small">
         <input type="hidden" name="score" value="3.5" readonly="readonly">
    </div>
    

    So I just set the value attribute with number coming from database query.