Star Rating Plugin for jQuery

What

An easy to use rating control. It takes a normal select box, and turns it into a rating that your users can click on. The select box is preserved so you can still bind on change, get, and set the value in the rating control. The image is controlled with CSS and a simple gif, so you can make it look like anything you need.

Download

Download (Moved to GitHub)
Download

Example

Here is a sample default rating control with no options.

Source

<select class="rating">
    <option value="0">Did not like</option>
    <option value="1">Ok</option>
    <option value="2" selected="selected">Liked</option>
    <option value="3">Loved!</option>
</select>
$(".rating").rating();

That’s it!

The select box is now replaced with the rating control. Each option you put in the select box is turned into a star, so you can easily have as many stars as you want. The value you set is the value that is returned for the selected star. The text becomes the title on the star.

You can turn the cancel button on and off, and change the value of the cancel button by passing it when you create the rating control.
$(".rating").rating({showCancel: true, cancelValue: null,})

You can make the control readonly by setting <select disabled="disabled">

Available Options:

  • showCancel: true : Should the cancel button be shown?
  • cancelValue: null : If cancel button is shown, what should it’s value be?
  • startValue: null : If no property has the ‘selected’ attribute, what value should be displayed?

Get Value

Click for Value

Source

<select name="myRating" class="rating" id="serialStar">
    <option value="0">Did not like</option>
    <option value="1">Ok</option>
    <option value="2">Liked</option>
    <option value="3">Loved!</option>
</select>
<span id="seralString">Click for Value</span>
<button onclick="$('#seralString').text( $('#serialStar').val() );">Serialize</button>
//Turn the Select into a rating
$(".rating").rating();
//Action on button click
$('#seralString').text( $('#serialStar').val() );

Bind $.serialize() On Change

Of course you don’t have to call $.serialize() here, you can call anything you want. It’s just a normal event. You even have access to the value with $("#serialStar2").val()!

Just click and I’ll change.

Source

<select name="myRating" class="rating" id="serialStar2">
    <option value="1">ALrighte</option>
    <option value="2">Ok</option>
    <option value="3">Getting Better</option>
    <option value="4">Pretty Good</option>
	<option value="5">Awesome</option>
</select>
<span id="serialString2">serialize that star rating!</span>
//Turn the select box into rating controls
$(".rating").rating();

//Show that we can bind on the select box
$("#serialStar2").bind("change", function(){
	$('#serialString2').text( $('#serialStar2').serialize() );
});

Setting the Value

There is one slight twist. In order to set the value programmatically, you have to set it, then trigger the change event on the select box.

<select name="myRating" class="rating" id="serialStar2">
    <option value="1">ALrighte</option>
    <option value="2">Ok</option>
    <option value="3">Getting Better</option>
    <option value="4">Pretty Good</option>
	<option value="5">Awesome</option>
</select>
<button onclick="$('#star3').val(4).change()"&gtSet to 'Pretty Good'</button&gt
//Turn the select box into rating controls
$(".rating").rating();

//Set the value to 4, then trigger change to update the rating.
$('#star3').val(4).change();

Bugfixes

  • IE Loading Problems
  • IE CSS Hover Over shows the wrong image

Todo

  • Allow disabling of the control
  • Allow global settings
  • Way to set the value without firing the change event
  • If no selected attribute is present, start with all stars blank
Share

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

60 Comments »

 
  • wordpress themes at the lowest prices…

    [...]Star Rating Plugin for jQuery | Write or Program[...]…

  • 輪圈 says:

    輪圈…

    [...]Star Rating Plugin for jQuery | Write or Program[...]…

  • schoonheidssalon leeuwarden…

    [...]Star Rating Plugin for jQuery | Write or Program[...]…

  • Normally I do not read article on blogs, however I wish to say that this write-up very forced me to check out and do it! Your writing style has been amazed me. Thanks, quite great post.

  • code de la route gratuit…

    [...]Star Rating Plugin for jQuery | Write or Program[...]…

  • Sampat says:

    Downloaded the source code from Github, but setting the value does not work in the sample. Gives an error: Uncaught ReferenceError: isDirty is not defined. Is there a fix for this?

  • Good day very nice website!! Guy .. Beautiful .. Wonderful .. I’ll bookmark your blog and take the feeds additionally?I’m glad to find a lot of helpful info right here in the post, we want develop extra techniques on this regard, thanks for sharing. . . . . .

  • Tom says:

    Hi, great plugin but could you please help me with something? I’ve read through the comments and believe this has been answered before, but i’m not sure i’m making the right changes – javascript is like greek to me, am more comfortable with PHP.

    Anyway, I need to have a ’0′ result if the form is submitted with no stars selected. I’ve tried adding an extra blank option in the , but this just adds an extra star again with the value of selected or unselected.

    I have the 21/09/2011 updated version – i’ve set the startValue to zero but no difference. Also tried adding the “selected” attribute to the option tag, but no difference – in any browser.

    thanks again,

  • polas says:

    Hi i have the question how to save rating data and stay on it ?
    cuz when i refresh page the ratings when i selected on click is canel itself.

  • jaszczomp says:

    @Sampat comment those 2 lines … works fine ;)

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv Enabled