Default

You need just to have a div to build the Raty.

<div></div>
$('div').raty();

Score

Used when we want starts with a saved rating.

$('div').raty({ score: 3 });

Score callback

If you need to start you score depending of a dynamic value, you can to use callback for it.
You can pass any value for it, not necessarily a data- value. You can use a field value for example.

<div data-score="1"></div>
$('div').raty({
  score: function() {
    return $(this).attr('data-score');
  }
});

Score Name

Changes the name of the hidden score field.

$('div').raty({ scoreName: 'entity[score]' });

Number

Changes the number of stars.

$('div').raty({ number: 10 });

Number callback

You can receive the number of stars dynamic using callback to set it.

<div data-number="3"></div>
$('div').raty({
  number: function() {
    return $(this).attr('data-number');
  }
});

Number Max

Change the maximum of start that can be created.

$('div').raty({
  numberMax : 5,
  number    : 100
});

Read Only

You can prevent users to vote. It can be applied with or without score and all stars will receives the hint corresponding of the selected star.
Stop the mouse over the stars to see:

$('div').raty({ readOnly: true, score: 3 });

Read Only callback

You can decide if the rating will be readOnly dynamically returning true of false on callback.

$('div').raty({
  readOnly: function() {
    return 'true becomes readOnly' == 'true becomes readOnly';
  }
});

No Rated Message

If readOnly is enabled and there is no score, the hint "Not rated yet!" will be shown for all stars. But you can change it.
Stop the mouse over the star to see:

$('div').raty({
  readOnly   : true,
  noRatedMsg : "I'am readOnly and I haven't rated yet!"
});

Half Show

You can represent a float score as a half star icon.
This options is just to show the half star. If you want enable the vote with half star on mouseover, please check the option half.
The round options showed belows is just for the icon, the score keeps as float always.

Enabled

The round rules are:

  • Down: score <= x.25 the star will be rounded down;
  • Half: score >= x.26 and <= x.75 the star will be a half star;
  • Up: score >= x.76 the star will be rounded up.
$('div').raty({ score: 3.26 });

Disabled

The rules becomes:

  • Down: score < x.6 the star will be rounded down;
  • Up: score >= x.6 the star will be rounded up;
$('div').raty({
  halfShow : false,
  score    : 3.26
});

Round

You can customize the round values of the halfShow option.
We changed the default interval [x.25 .. x.76], now x.26 will round down instead of to be a half star.
Remember that the full attribute is used only when halfShow is disabled.
You can specify just the attribute you want to change and keeps the others as default.

$('div').raty({
  round : { down: .26, full: .6, up: .76 },
  score : 3.26
});

Half

Enables the half star mouseover to be possible vote with half values.
If you want to vote with more precison than half value, please check the option precision.

$('#star').raty({ half: true });

Star Half

Changes the name of the half star.
Pay attention, when you want specify a different icon with a different directory, you must to set the path option to null to avoid it to be prepended on your path and, consequently, specify all other icons with explicit original path.

$('div').raty({
  half     : true,
  starHalf : 'star-half-mono.png'
});

Click

Callback to handle the score and the click event on click action.
You can mension the Raty element (DOM) itself using this.

$('div').raty({
  click: function(score, evt) {
    alert('ID: ' + $(this).attr('id') + "\nscore: " + score + "\nevent: " + evt);
  }
});

Hints

Changes the hint for each star by it position on array.
If you pass null, the score value of this star will be the hint.
If you pass undefined, this position will be ignored and receive the default hint.

$('div').raty({ hints: ['a', null, '', undefined, '*_*']});

Path

Changes the path where your icons are located.
Set it only if you want the same path for all icons.
Don't mind about the last slash of the path, if you don't put it, it will be setted for you.

$('div').raty({ path: 'assets/images' });

Now we have the following full paths: assets/images/star-on.png, assets/images/star-off.png and so.

Star Off and Star On

Changes the name of the star on and star off.

$('div').raty({
  starOff : 'off.png',
  starOn  : 'on.png'
});

Cancel

Add a cancel button on the left side of the stars to cacel the score.
Inside the click callback the argument code receives the value null when we click on cancel button.

$('div').raty({ cancel: true });

Cancel Hint

Like the stars, the cancel button have a hint too, and you can change it.
Stop the mouse over the cancel button to see:

$('div').raty({
  cancel     : true,
  cancelHint : 'My cancel hint!'
});

Cancel Place

Changes the cancel button to the right side.

$('div').raty({
  cancel      : true,
  cancelPlace : 'right'
});

Cancel off and Cancel On

Changes the on and off icon of the cancel button.

$('div').raty({
  cancel    : true,
  cancelOff : 'cancel-off.png',
  cancelOn  : 'cancel-on.png'
});

Icon Range

It's an array of objects where each one represents a custom icon.
The range attribute is until wich position the icon will be displayed.
The on attribute is the active icon.
The off attribute is the inactive icon.

$('div').raty({
  iconRange: [
    { range: 1, on: '1.png', off: '0.png' },
    { range: 2, on: '2.png', off: '0.png' },
    { range: 3, on: '3.png', off: '0.png' },
    { range: 4, on: '4.png', off: '0.png' },
    { range: 5, on: '5.png', off: '0.png' }
  ]
});

You can use an interval of the same icon jumping some number.
The range attribute must be in an ascending order.
If the value on or off is omitted then the attribute starOn and starOff will be used.

$('div').raty({
  starOff   : '0.png',
  iconRange : [
    { range : 1, on: '1.png' },
    { range : 3, on: '3.png' },
    { range : 5, on: '5.png' }
  ]
});

Now we have all off icons as 0.png, icons 1 and 2 as 1.png, icon 3 as 3.png and icons 4 and 5 as 5.png.

Size

The size in pixel of the icon you will to use.
It does not change the icon size, just the icons's wrap space to fit it.

$('div').raty({
  cancel : true,
  half   : true,
  size   : 24
});

Width

By default Raty calculates the width calculating the size of the stars plus the spaces.
But for some reason the calculated width not fits on your layout, you can change it manually.
If you want to avoid that Raty applies the style width on you wrapper, set it to false.

$('div').raty({ width: 150 });

Target

Some place to display the hints or the cancelHint.

$('div').raty({
  cancel : true,
  target : '#hint'
});

Your target can be a div.

<div id="hint"></div>

Your target can be a text field.

<input id="hint" type="text" />

Your target can be a textarea.

<textarea id="hint"></textarea>

Your target can be a select.

<select id="hint">
  <option value="">--</option>
  <option value="bad">bad</option>
  <option value="poor">poor</option>
  <option value="regular">regular</option>
  <option value="good">good</option>
  <option value="gorgeous">gorgeous</option>
</select>

Target Type

You have the option hint or score to chosse.
You can choose to see the score instead the hints using the value score.
For the cancel button the value is empty.

$('div').raty({
  cancel     : true,
  target     : '#hint',
  targetType : 'number'
});

Target Keep

If you want to keep the score into the hint box after you do the rating, turn on this option.

$('div').raty({
  cancel     : true,
  target     : '#hint',
  targetKeep : true
});

Target Text

Normally all target is keeped blank if you don't use the targetKeep option.
If you want a default content you can use this option.

$('div').raty({
  target     : '#hint',
  targetText : '--'
});

Target Format

You can choose a template to be merged with your hints and displayed on target.

$('div').raty({
  target       : '#hint',
  targetFormat : 'Rating: {score}'
});

Mouseover

You can handle the action on mouseover.
The arguments is the same of the click callback.
The options target, targetFormat, targetKeep, targetText and targetType are abstractions of this callback. You can do it by yourself.

$('div').raty({
  mouseover: function(score, evt) {
    alert('ID: ' + $(this).attr('id') + "\nscore: " + score + "\nevent: " + evt);
  }
});

Mouseout

You can handle the action on mouseout.
The arguments is the same of the mouseover callback.

$('div').raty({
  mouseout: function(score, evt) {
    alert('ID: ' + $(this).attr('id') + "\nscore: " + score + "\nevent: " + evt);
  }
});

Share it!

  1. Marc Stein 3 days ago

    Hi Washington,

    I'm having a styling issue with Raty that I've been unable to solve.

    If you take a look at http://recippia.herokuapp.com/test.html you can see that the stars stack vertically rather than displaying inline. I'v tried overriding the img tag formatting in style.css but this doesn't seem to do the trick. Any thoughts as to how to resolve this?

    Many thanks in advance,

    Marc

    Great plugin, BTW!

  2. Marc Stein 3 days ago

    Hi Washington,

    I'm having a styling issue with Raty that I've been unable to solve.

    If you take a look at http://recippia.herokuapp.com/test.html you can see that the stars stack vertically rather than displaying inline. I'v tried overriding the img tag formatting in style.css but this doesn't seem to do the trick. Any thoughts as to how to resolve this?

    Many thanks in advance,

    Marc

    Great plugin, BTW!

  3. Surjit Sidhu 13 days ago

    I want to display "based on x reviews" after stars. how can do this ?
    i tried to modify js function, but does not find exact line to change

  4. Surjit Sidhu 13 days ago

    I want to display "based on x reviews" after stars. how can do this ?
    i tried to modify js function, but does not find exact line to change

  5. sudarshan 17 days ago

    Hello sir, nice work.
    I have one doubt I am not getting img of stars when I used it in Codeigniter project. Can you tell me where can I provide Base url for img folder??
    Thanks.

    1. Vlad 14 days ago

      sudarshan,

      Had the same issue,

      $('#star').raty({
      path: 'img/',
      });

      if that doesnt work try,

      jQuery(function($) {
      $('#star').raty({
      path: 'img/',
      });
      });

  6. Cabel 1 month ago

    Any easy way to make the stars smaller? Other than that, what a great plugin, keep up the great work.

  7. ISEE 1 month ago

    Hi.
    Now, we can get the score from the callbcak function when the raty was initialized, suck as:

    $('div').raty({
    click: function(score, evt) {
    alert('core: " + score);
    }
    });

    but can we get the score in the other function?

  8. Darren Teng 1 month ago

    Hi Washington, can I set my own title for each star ,such as 'good'=>'Strongly agree','bad'=>'Strongly disagree'?
    Thanks for your great work!

  9. Guilherme Duailibe 1 month ago

    Excelente plugin! Estou usando. Obrigado!

    1. Washington Botelho author 1 month ago

      Guilherme Duailibe,

      Thank you! (:

  10. Daniel Better 1 month ago

    Hi Washington, first of all, great plugin, gj!
    Secondly, is there a way to enlarge the stars even more then 24px as seen here?

    Regards, Daniel.

    1. Washington Botelho author 1 month ago

      Daniel Better,

      Yes, just use your new icon and set the value of it as px on size property.

  11. Nishit 1 month ago

    Hi Washington,

    I would like to thank you for this plugin. It works flawlessly. Great work.

    Regards.

    1. Washington Botelho author 1 month ago

      Nishit,

      Thanks man! (:

  12. Brian 2 months ago

    I have a need for multiple ratings on a page. For example, individual ratings for things such as "Price," "Value" and "Quality." Is there a way to specify scoreName for each instance of a rating? Excellent plugin, btw.

    1. Brian 2 months ago

      In the init function, I have changed:

      this.opt = $.extend(true, {}, $.fn.raty.defaults, settings);
      

      to

      this.opt = $.extend(true, {}, $.fn.raty.defaults, settings, $(this).data());
      

      This allows me to pass instance variables via the div. Example:

      <div class="my-rating" data-score-name="rating[quality]"></div>
      

      Perhaps a consideration to add to the master?

      1. Washington Botelho author 1 month ago

        Brian,

        Could you open a issue to create a scoreName callback?
        This way we can pass a function to scoreName and receive it via data or whatever.

        Thanks.

  13. Istvan Szollosi 3 months ago

    Hi,
    I tried to use your plugin in my blog/web-shop but I got these errors:

    Uncaught TypeError: Object #<Object> has no method 'on' jquery.raty.min.js:12
    a._bindClick jquery.raty.min.js:12
    a._binds jquery.raty.min.js:12
    (anonymous function) jquery.raty.min.js:12
    E.extend.each VM4322:1
    E.fn.E.each VM4322:1
    a.init jquery.raty.min.js:12
    b.fn.raty jquery.raty.min.js:12
    (anonymous function) produs.php?produsid=21:245
    (anonymous function) VM4322:1
    (anonymous function) VM4322:1
    E.extend.each VM4322:1
    E.extend.ready VM4322:1
    event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
    
    1. Washington Botelho author 1 month ago

      Istvan Szollosi,

      Check if your jQuery was imported before the Raty call.
      Check if you jQuery version has the new method on.

      This problem is about import, about jQuery, not about Raty.

  14. Mark 3 months ago

    Hi

    Is there a way of easily posting the data to a rails application? I have added the call to my application to get the images and the java but every time I post my form my defined attribute doesn't save the the rating to the DB. Heres my code :

    <div id="star" data-score="1">
      <%= :review_rating %>
    </div>
    
    $('#star').raty({
      score: function() {
        return $(this).attr(':review_rating')
      }
    });
    

    Forgive me if it seems basic but I know very little about J query and am at beginner level. The :review_rating is my rails attribute that Im trying to commit the score to.

    Please let me know.

    Many Thanks

    1. Washington Botelho author 1 month ago

      Mark,

      The value send by jQuery Raty is a hidden field, not what you put inside the div.
      This content inside the div is ignored, because Raty will override it, then you <%= :review_rating %> is useless.
      By default the hidden field that holds the score is called score, but you can change it if you need.

  15. Brandon 3 months ago

    Awesome plugin! Super easy to use and dropped right into my rails app for a reviews rating system. Thanks!

    1. Washington Botelho author 1 month ago

      Brandon,

      Great! (:

  16. Daniel Weller 3 months ago

    Here is a link to a tutorial for ruby on rails. If anyone can get it to fully work let me know! I've been able to only get the update stars to work. (Average rating isn't working)

    http://paweljaniak.co.za/2013/07/25/5-star-ratings-with-rails/

    1. Washington Botelho author 3 months ago

      Daniel Weller,

      Thanks for the contribution.

  17. Max 3 months ago

    @Roosevelt: luckily you find the download on his github page as well: https://github.com/wbotelhos/raty

    1. Washington Botelho author 3 months ago

      Max,

      Thanks for the help. (:

  18. Roosevelt P 3 months ago

    Your download link to the plugin is throwing a 404 error.

    1. Washington Botelho author 3 months ago

      Roosevelt P,

      I fixed it. Thanks!

  19. Bill 3 months ago

    Hi,

    Is there a way to check and see if a visitor has already voted & if so, then set it to read only?

    Also, I would like to store the rating & number of votes in a mySQL database, how could I do that?

    1. Washington Botelho author 3 months ago

      Bill,

      You can save it into database.
      When the user click on star, it actives the click callback, inside it you can get the score and save it via ajax.

      Or you can just let the user to vote and create a submit button that will send the value keeped into hidden field named score by default.

  20. Tj 3 months ago

    Hi, How to save previous ratings ?? How to download and install this plugin ?

    please help

    1. Washington Botelho author 3 months ago

      Tj,

      To save the last rating, you need to keep it on your database.
      To instal, just import the files e use the code showed on this page.

  21. Lanamaja 4 months ago

    Hi,
    how can i add this on a wordpress page? and/or include into a contact form?
    If i want the plugin to open up different links or display messages depending on the number of stars the user clicked, how could i manage this
    with your plugin?

    Thanks in advance

    1. Washington Botelho author 3 months ago

      Lanamaja,

      You will use the same code. But where put it, I can't help you, because I don't know about wordpress coding.

      To open whatever you want, use the callback click, inside it you can get the score and do your logic.

  22. Amarpreet 4 months ago

    Hi,

    Could you help me with how i can post the rating using ajax.

    1. Washington Botelho author 4 months ago

      Amarpreet,

      You can do it from inside the click callback.
      Get the score value and manipulate it with jQuery.

      Ajax POST reference: http://api.jquery.com/jquery.post

  23. Washington Botelho 4 months ago

    first

*
*
*
Answering:
(cancel)
Format your code using Markdown.