Jump to content

Rating with image


toolman

Recommended Posts

Hi there,

 

I have a rating script which displays ratings as numbers: 1, 1.5, 2, 2.5 etc up to 10.

 

I want to display star images now instead of numbers, but up to 5 instead of 10.

 

This is the code I have which outputs the numbers:

 

if ( $(relData).attr("responses") > 10 )

                                                overallRating = Ratings.RoundToHalf( ( convenience + quality + rebook )/3 );

 

How would I do this? I am not good with Javascript.

 

Alternatively, is there a way I can replace the numbers with images, for example if the rating is 5.5 i can replace it with <img src="5star.jpg" /> ?

 

Thanks!

Link to comment
Share on other sites

If it were me, I'd look into changing how it's output from wherever it is outputting the original value.

 

But here is an example of how to do it:

 

<span class='rating'>10</span>
<span class='rating'>4.5</span>
<span class='rating'>2</span>

<script type='text/javascript'>
$(document).ready(function() {
  $('.rating').each(function() {
    var rating = Math.ceil(Number($(this).html()) / 2);
    $(this).html('<img src="'+rating+'"star.jpg" />');
  });
});
</script>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.