Geekpedia Programming Tutorials






Dynamic 5 Star Rating Script

A dynamic 5 star rating script that can be used along with a server-side language and AJAX to allow visitors to submit their vote in an interactive manner.

On Wednesday, February 14th 2007 at 06:25 PM
By Sorin Sodolescu (View Profile)
****-   (Rated 4 with 297 votes)
Contextual Ads
More JavaScript Resources
Advertisement

JavaScript Animation

Sample See this code in action

The only HTML elements that we're going to need are 5 images and one div.

<body onload="loadStars()">
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="1" style="width:30px; height:30px; float:left;" />
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="2" style="width:30px; height:30px; float:left;" />
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="3" style="width:30px; height:30px; float:left;" />
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="4" style="width:30px; height:30px; float:left;" />
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="5" style="width:30px; height:30px; float:left;" /><br /><br />
<div id="vote" style="font-family:tahoma; color:red;"></div>
</body>

Let's move on to the JavaScript code. First we'll declare some variables that we will use later. As you've seen earlier, the body tag calls the loadStars() function. This function preloads the images used to represent the star when it is in focus and when it is not.

<script type="text/javascript">
<!--
var set=false;
var v=0;
var a;
function loadStars()
{
   star1 = new Image();
   star1.src = "star1.gif";
   star2 = new Image();
   star2.src= "star2.gif";
}

Now let's write the function that will change the stars on mouse over. If we point to star number 1, it should be highlighted; if we point to star number 2, star 2 and 1 should be highlighted, and so on. So we need to get the image's ID in order to know how many and what stars to highlight. The images use the onmouseover event to trigger the function, and the parameter is it's ID. I've also used an y variable (which is the current ID + 1) as a stop condition in the For loops that this function will use. Also you will see an set variable, i'll explain that later in this tutorial.

function highlight(x)
{
   if (set==false)
   {
      y=x*1+1
      switch(x)
      {
         case "1":
            document.getElementById(x).src= star2.src;
            document.getElementById('vote').innerHTML="one star";
            break;
         case "2":
            for (i=1;i<y;i++)
            {
               document.getElementById(i).src= star2.src;
            }
            document.getElementById('vote').innerHTML="two stars"
            break;
         case "3":
            for (i=1;i<y;i++)
            {
               document.getElementById(i).src= star2.src;
            }
            document.getElementById('vote').innerHTML="three stars"
            break;
         case "4":
            for (i=1;i<y;i++)
            {
               document.getElementById(i).src= star2.src;
            }
            document.getElementById('vote').innerHTML="four stars"
            break;
         case "5":
            for (i=1;i<y;i++)
            {
               document.getElementById(i).src= star2.src;
            }
            document.getElementById('vote').innerHTML="five stars"
            break;
      }
   }
}

Depending on the image's ID, the switch statement will run one of the For loops. If the ID is "2", for example, the For loop will run from i=1 to i<3, changing the first and second star and writing "2 stars" in a div. If the ID is "3" a For loop will be run that will write "3 stars" in a div and change star number 1, 2 and 3; and so on for the rest of the IDs.
We now have to make a function that will make the stars not be highlighted once they lose focus. For this, we will use the onmouseout event: onmouseout="losehighlight(this.id)".

function losehighlight(x)
{
   if (set==false)
   {
      for (i=1;i<6;i++)
      {
         document.getElementById(i).src=star1.src;
         document.getElementById('vote').innerHTML=""
      }
   }
}

This function makes all of the stars appear as not selected, and changes the content of the div to an empty string.
Now we have to write a function that will make the stars remain highlighted once clicked on. So, the event will be onclick="setStar(this.id)".
Finally it's time to explain the set variable. To avoid rating one way and then rating again we've used this set variable. If no star has been clicked the variable will have the false value; after you rate (click on a star) the variable will become true, and the If condition in the functions will not allow to rate again (or highlight stars).

function setStar(x)
{
   y=x*1+1
   if (set==false)
   {
      switch(x)
      {
         case "1":
            a="1"
            flash(a);
            break;
         case "2":
            a="2"
            flash(a);
            break;
         case "3":
            a="3"
            flash(a);
            break;
         case "4":
            a="4"
            flash(a);
            break;
         case "5":
            a="5"
            flash(a);
            break;
      }
      set=true;
      document.getElementById('vote').innerHTML="Thank you for your vote!"
   }
}

You can see that set becomes true at the end of the function. You can also see the flash() function being called. That function makes the star remain selected, and it also makes it blink a few times, for a nice effect.

function flash()
{
   y=a*1+1
   switch(v)
   {
      case 0:
         for (i=1;i<y;i++)
         {
            document.getElementById(i).src= star1.src;
         }
         v=1
         setTimeout(flash,200)
         break;
      case 1:
         for (i=1;i<y;i++)
         {
            document.getElementById(i).src= star2.src;
         }
         v=2
         setTimeout(flash,200)
         break;
      case 2:
         for (i=1;i<y;i++)
         {
            document.getElementById(i).src= star1.src;
         }
         v=3
         setTimeout(flash,200)
         break;
      case 3:
         for (i=1;i<y;i++)
         {
            document.getElementById(i).src= star2.src;
         }
         v=4
         setTimeout(flash,200)
         break;
      case 4:
         for (i=1;i<y;i++)
         {
            document.getElementById(i).src= star1.src;
         }
         v=5
         setTimeout(flash,200)
         break;
      case 5:
         for (i=1;i<y;i++)
         {
            document.getElementById(i).src= star2.src;
         }
         v=6
         setTimeout(flash,200)
         break;
   }
}
-->
</script>

The "v" variable will make this function run for 6 times. You can see here the switch statement and for loops that we used in the highlight function(). The difference is the setTimeout() method that will call the function after 200 miliseconds. So basically, when this function is called, v is 0 and the stars' appearance change, then v becomes 1 and the function is called again after 200 miliseconds changing the appearance of the stars again (as stated in case 1) and v becomes 2. This will go on until v becomes 6, creating a blinking effect.

You can use the code below to create your dynamic star rating system:

<html>
<head>
<title>Dynamic 5 Star Rating Script </title>
<script type="text/javascript">
<!--
var set=false;
var v=0;
var a;
function loadStars()
{
   star1 = new Image();
   star1.src = "star1.gif";
   star2 = new Image();
   star2.src= "star2.gif";
}

function highlight(x)
{
   if (set==false)
   {
   y=x*1+1
   switch(x)
   {
   case "1": document.getElementById(x).src= star2.src;
   document.getElementById('vote').innerHTML="one star";
   break;
   case "2":for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star2.src;
   }
   document.getElementById('vote').innerHTML="two stars"
   break;
   case "3":for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star2.src;
   }
   document.getElementById('vote').innerHTML="three stars"
   break;
   case "4":for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star2.src;
   }
   document.getElementById('vote').innerHTML="four stars"
   break;
   case "5":for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star2.src;
   }
   document.getElementById('vote').innerHTML="five stars"
   break;
   }
   }
}
function losehighlight(x)
{
   if (set==false)
   {
   for (i=1;i<6;i++)
   {
   document.getElementById(i).src=star1.src;
   document.getElementById('vote').innerHTML=""
   }
   }
   }
   function setStar(x)
   {
   y=x*1+1
   if (set==false)
   {
   switch(x)
   {
   case "1": a="1"
   flash(a);
   break;
   case "2": a="2"
   flash(a);
   break;
   case "3": a="3"
   flash(a);
   break;
   case "4":a="4"
   flash(a);
   break;
   case "5":a="5"
   flash(a);
   break;
   }
   set=true;
   document.getElementById('vote').innerHTML="Thank you for your vote!"
   }
}
function flash()
{
   y=a*1+1
   switch(v)
   {
   case 0:
   for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star1.src;
   }
   v=1
   setTimeout(flash,200)
   break;
   case 1:
   for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star2.src;
   }
   v=2
   setTimeout(flash,200)
   break;
   case 2:
   for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star1.src;
   }
   v=3
   setTimeout(flash,200)
   break;
   case 3:
   for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star2.src;
   }
   v=4
   setTimeout(flash,200)
   break;
   case 4:
   for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star1.src;
   }
   v=5
   setTimeout(flash,200)
   break;
   case 5:
   for (i=1;i<y;i++)
   {
   document.getElementById(i).src= star2.src;
   }
   v=6
   setTimeout(flash,200)
   break;
   }
}
-->
</script>
</head>
<body onload="loadStars()">
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="1" style="width:30px; height:30px; float:left;" />
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="2" style="width:30px; height:30px; float:left;" />
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="3" style="width:30px; height:30px; float:left;" />
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="4" style="width:30px; height:30px; float:left;" />
<img src="star1.gif" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" id="5" style="width:30px; height:30px; float:left;" /><br /><br />
<div id="vote" style="font-family:tahoma; color:red;"></div>
</body>
</html>

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by ct on Tuesday, April 17th 2007 at 09:01 PM

hello. ive copy paste ur code into my webpage, however the stars cannot be seen. can you please help me? thank you :)

by Sorin Sodolescu on Wednesday, April 18th 2007 at 09:44 AM

hello ct. i think that the stars can't be seen because you either don't have the star image at all (star1.gif and star2.gif) or you've misspelled their names in the script. Hope you'll find this helpfull, but if you still have problems, write in again.

by ct on Wednesday, April 18th 2007 at 11:00 PM

thanks for the reply! ya, ive replaced the star image already. so its ok now. however can i know whether this star rater system can be used once or many times? as in i would like users to rate different videos.

by Sorin Sodolescu on Thursday, April 19th 2007 at 05:05 AM

You need to modify the script to get it to work the way you want to.

by The Gamble Society on Friday, April 27th 2007 at 07:47 AM


It doesnt tell u how many ppl have rated it or nothin.. There are much better ones out there!

by Sorin Sodolescu on Friday, April 27th 2007 at 02:58 PM

This script doesn't tell how many people have voted because it's writen in javascript, a client side scripting language. Javascript simply can't count the votes. To do that you need to write some (simple) server side code. You've missed the whole purpose of this tutorial - "A dynamic 5 star rating script that can be used along with a server-side language and AJAX to allow visitors to submit their vote in an interactive manner." - this is only the visual, interactive part. You may want to read some php/asp.net tutorials.

by Akshay Kumar Vishnoi on Saturday, December 1st 2007 at 01:38 AM

I think it is best and simple rating system.
I would like thanks to developer as well as this site, providing good star rating for new developer.

Thanks and Regards
Akshay Kumar Vishnoi
WebDeveloper.
India
Ghaziabad(U.P)

by alex on Saturday, December 1st 2007 at 03:30 PM

Hi... Where do I download the stars from? Thanks

by Akshay Kumar on Tuesday, December 4th 2007 at 09:12 AM

Hi alex you can save star from \"See this code in action \" save picture as.

by dd on Sunday, January 27th 2008 at 06:51 AM

Nice script... one thing it is missing which has nothing to do with the dynamic server side scripting is the lack of cookies - the rater allows me to vote as many times as i want..

by ram on Sunday, March 16th 2008 at 12:21 PM

Hi really good script , but i think it was not blocking ip

iam posting this comment in html page , can any one tell me how to put this type of comment posting in normal html pages

pls tell me at mails.accounts@gmail.com


by chaithanya on Monday, April 28th 2008 at 06:08 AM

good one...

by Harish Kumar on Friday, May 9th 2008 at 07:38 AM

I was trying to build my own five-star rating script but then I thought , somebody must have figured this out , and did a search and lo ! I am here and it works very well ! Thank You. You saved me at least an hour's time programming.

by Frances on Tuesday, June 3rd 2008 at 12:46 PM

This looks really good thanks

by Aqeel on Wednesday, September 10th 2008 at 10:02 AM

it is wonderful and easy. thank you very much.

by ollo on Sunday, September 14th 2008 at 02:56 PM

Just a remark: HTML doesn't officially support ids starting with numbers. Though most of the time you're fine, it can lead to problems with some browsers.

by i made this :) on Wednesday, September 17th 2008 at 08:04 AM

var num = 0;
function setStar(n)
{
var element = document.getElementById('star_select');
element.style.backgroundPosition = '-' (105 - (n * 21)) 'px 0';
}
function resetStar(){setStar(num);}
function saveStar(n)
{
var form = document.getElementById('formReview');
form.rating.value = n;
num = n;
}


<div id="star_select">
<img src='images/transparent.gif' width="21" height="21" onmouseover="setStar(1);" onmouseout="resetStar();" onmouseup="saveStar(1);"/><img src='images/transparent.gif' width="21" height="21" onmouseover="setStar(2);" onmouseout="resetStar();" onmouseup="saveStar(2);"/><img src='images/transparent.gif' width="21" height="21" onmouseover="setStar(3);" onmouseout="resetStar();" onmouseup="saveStar(3);"/><img src='images/transparent.gif' width="21" height="21" onmouseover="setStar(4);" onmouseout="resetStar();" onmouseup="saveStar(4);"/><img src='images/transparent.gif' width="21" height="21" onmouseover="setStar(5);" onmouseout="resetStar();" onmouseup="saveStar(5);"/>
</div>

by Aqeel on Wednesday, September 17th 2008 at 09:44 PM

When I used smaller stars it turned out that there are big spaces between them.how can i make stars close to each other and a little smaller that they really appear in your script.

by kjb31ca on Tuesday, October 21st 2008 at 01:37 PM

Great script... but how would I use multiple 5-star ratings on a single page using a loop. I know I would have to use an array, but I'm not too good on the syntax. Any help?

by aaa on Friday, October 31st 2008 at 12:34 AM

<script>alert("hello");</script>

by Kryptonite-Dove on Friday, November 21st 2008 at 10:33 AM

This code seems quite long winded to me and would be quite time consuming to swap bits round.
I recently wanted one of these for the new website i'm creating and whoever posted in comments they wanted it done in a loop is bang on. That is the way to do it.

If you would like to review my attempt i've put a package together here: http://www.insecureinc.com/beta/widgets/starrank/index.php

The above is a working example of my AJAX star ranking code. You can view source to download the JS and CSS for yourselves and put the backend together in whatever sever language you feel comfortable in!

by sherri on Sunday, November 30th 2008 at 11:47 AM

I downloaded stars with getright browser works great just wondering how to make it smaller

by Kryptonite Dove on Friday, December 12th 2008 at 02:02 PM

See mine in action here: http://www.kryptonite-dove.com (just click on any article) it's open source so feel free to download it the js file that makes it all work and do the back end in your prefered server language.

PS Sherri you need some image editing software to make the images smaller. Photoshop for example.

by theredone89 on Monday, December 15th 2008 at 11:50 AM

@Kryptonite-Dove

" http://www.insecureinc.com/beta/widgets/starrank/index.php "
can i get the php script pls :)

by Kryptonite Dove on Monday, December 15th 2008 at 03:22 PM

sure can mate email me my address is at that site im still building it but scroll down on the blog page or if not its richduncan@gmail.com and ill send you it on

in future ill have a section where you can view the code or download so check back in future :D its just taking me ages to crack on with it because of xmas!

by Kryptonite Dove on Monday, December 15th 2008 at 03:22 PM

sure can mate email me my address is at that site im still building it but scroll down on the blog page or if not its richduncan@gmail.com and ill send you it on

in future ill have a section where you can view the code or download so check back in future :D its just taking me ages to crack on with it because of xmas!

by Fred on Monday, December 22nd 2008 at 09:27 AM

This code is so awful I felt the need to post a comment. There is so much code duplication!

Instead of having a switch statement with cases for every concrete value, just use the variable that you already have, 'x'. So for example:

switch(x)
{
case "1": a="1"
flash(a);
break;
case "2": a="2"
flash(a);
... etc ...

becomes:

flash(x)

Simple, right?

by Kryptonite Dove on Monday, December 22nd 2008 at 09:32 AM

agreed. this is horrible code!

by qwerty on Monday, February 23rd 2009 at 07:54 PM

thanks a lot for this script! very useful. The first time I tried it, the stars wouldn't show up.
Instead I used clipart pics like these

http://www.freeclipartnow.com/d/41631-1/smooth-star.jpg

and

http://www.clipartguide.com/_small/0808-0801-1116-0931.jpg

You also have to change the code at the top it will return to its normal state.

by elvinson on Tuesday, March 31st 2009 at 08:08 AM

it is working. but how to save the rating. or where the ratings are saved

by Shentall on Saturday, April 11th 2009 at 11:56 AM

Thanks for the code.
I would like to know where I could download star1.gif and star2.gif.

Thanks again,

Regards,
Shentall

by Kryptonite Dove on Sunday, April 12th 2009 at 06:38 AM

Are you joking Shentall ????

by fahad mahmood on Tuesday, April 28th 2009 at 02:15 AM

yeah, it can be a joke Kryptonite Dove

by nagesh on Monday, May 18th 2009 at 01:16 PM

it is working. but how to save the rating. or where the ratings are saved

by nagesh on Monday, May 18th 2009 at 01:16 PM

it is working. but how to save the rating. or where the ratings are saved

by Suraj on Wednesday, May 27th 2009 at 09:37 AM

Hi,
i found one link regardin the 5 Star Rating option but anm finding some problem in tracking the ratings by the user in the database for the individual documents. If you have any idea i will be really great...
and the link i got the code is
http://www.reignwaterdesigns.com/ad/tidbits/rateme/
but not able to track into database..
Regards Suraj

by Suraj on Wednesday, May 27th 2009 at 09:37 AM

Hi,
i found one link regardin the 5 Star Rating option but anm finding some problem in tracking the ratings by the user in the database for the individual documents. If you have any idea i will be really great...
and the link i got the code is
http://www.reignwaterdesigns.com/ad/tidbits/rateme/
but not able to track into database..
Regards Suraj

by Suraj on Wednesday, May 27th 2009 at 09:37 AM

Hi,
i found one link regardin the 5 Star Rating option but anm finding some problem in tracking the ratings by the user in the database for the individual documents. If you have any idea i will be really great...
and the link i got the code is
http://www.reignwaterdesigns.com/ad/tidbits/rateme/
but not able to track into database..
Regards Suraj

by Suraj on Wednesday, May 27th 2009 at 09:37 AM

Hi,
i found one link regardin the 5 Star Rating option but anm finding some problem in tracking the ratings by the user in the database for the individual documents. If you have any idea i will be really great...
and the link i got the code is
http://www.reignwaterdesigns.com/ad/tidbits/rateme/
but not able to track into database..
Regards Suraj

by Suraj on Thursday, May 28th 2009 at 02:04 AM

Hi,
i found one link regarding the 5 Star Rating option but an finding some problem in track the ratings by the user in the database for the individual documents. If you have any idea it will be really great...
and the link i got the code is
http://www.reignwaterdesigns.com/ad/tidbits/rateme/
but not able to track or save into database..
Hope u will do reply..
Regards Suraj

by suraj on Thursday, May 28th 2009 at 05:26 AM

Hi,
i found one link regarding the 5 Star Rating option but an finding some problem in track the ratings by the user in the database for the individual documents. If you have any idea it will be really great...
and the link i got the code is
http://www.reignwaterdesigns.com/ad/tidbits/rateme/
but not able to track or save into database..
Hope u will do reply..
Regards Suraj

by Prasad G.V.S.N on Sunday, June 14th 2009 at 09:52 AM

Hi guys,
When the user already rated i need to show to are highlighted how to write script for that.When i mouse overed the highlited ones are going how to make them highlighted

by Prasad G.V.S.N on Sunday, June 14th 2009 at 09:52 AM

Hi guys,
When the user already rated i need to show to are highlighted how to write script for that.When i mouse overed the highlited ones are going how to make them highlighted

by Prasad G.V.S.N on Sunday, June 14th 2009 at 09:52 AM

Hi guys,
When the user already rated i need to show to are highlighted how to write script for that.When i mouse overed the highlited ones are going how to make them highlighted

by Prasad G.V.S.N on Sunday, June 14th 2009 at 09:52 AM

Hi guys,
When the user already rated i need to show to are highlighted how to write script for that.When i mouse overed the highlited ones are going how to make them highlighted

by Prasad G.V.S.N on Sunday, June 14th 2009 at 09:53 AM

Hi guys,
When the user already rated i need to show to are highlighted how to write script for that.When i mouse overed the highlited ones are going how to make them highlighted

by Prasad G.V.S.N on Sunday, June 14th 2009 at 09:53 AM

Hi guys,
When the user already rated i need to show to are highlighted how to write script for that.When i mouse overed the highlited ones are going how to make them highlighted

by Amit on Friday, July 17th 2009 at 02:25 PM

Hello,

Thanks for Developer.

i need to store these value into database.

Amit
Sr.programmer
Iprotek solutions

by Amit on Friday, July 17th 2009 at 02:25 PM

Hello,

Thanks for Developer.

i need to store these value into database.

Amit
Sr.programmer
Iprotek solutions

by Carito on Tuesday, August 11th 2009 at 09:51 AM

Thanks a lot! It's great!

by sinojelly on Tuesday, August 25th 2009 at 09:06 AM

I have made the code much smaller....

<html>
<head>
<title>Dynamic 5 Star Rating Script </title>
<script type="text/javascript">
<!--
var set=false;
var v=1; // 初始值是已投票的平均值???
var RatingLevel = new Array("poor", "below average", "average", "above average", "excellent");
function loadStars()
{
star1 = new Image();
star1.src = "star1.png";
star2 = new Image();
star2.src= "star2.png";
setStar(v);
//alert("test");
}
function highlight(x)
{
if (set==false)
{
y=x*1 1;
for (i=1;i<y;i )
{
document.getElementById(i).src= star2.src;
}
document.getElementById('vote').innerHTML=RatingLevel[x - 1];
}
}
function losehighlight(x)
{
if (set==false)
{
for (i=1;i<6;i )
{
document.getElementById(i).src=star1.src;
document.getElementById('vote').innerHTML=""
}
}
}
function setStar(n)
{
y = n * 1 1;
for (i=1;i<y;i )
{
document.getElementById(i).src= star2.src;
}
}
function saveStar(n)
{
v = n; // 应该是新的平均值
setStar(v);
set=true;
document.getElementById('vote').innerHTML="Thank you for your vote!";
alert(n);
}
-->
</script>
</head>
<body onload="loadStars()">
<img src="star1.png" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" onmouseup="saveStar(id)" id="1" style="width:30px; height:30px; float:left;" />
<img src="star1.png" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" onmouseup="saveStar(id)" id="2" style="width:30px; height:30px; float:left;" />
<img src="star1.png" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" onmouseup="saveStar(id)" id="3" style="width:30px; height:30px; float:left;" />
<img src="star1.png" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" onmouseup="saveStar(id)" id="4" style="width:30px; height:30px; float:left;" />
<img src="star1.png" onmouseover="highlight(this.id)" onclick="setStar(this.id)" onmouseout="losehighlight(this.id)" onmouseup="saveStar(id)" id="5" style="width:30px; height:30px; float:left;" />
<div id="vote"></div>
</body>
</html>

by cccd on Thursday, February 25th 2010 at 02:24 AM

Thank you guys for your help but can you please tell me where I have to write my pictures url and were should be my pics(in desktop)???

thank you

by Haris on Thursday, February 25th 2010 at 08:24 AM

Thanks for the script!
But how do I restrict a user from same IP to vote once?

by Divakar on Friday, March 19th 2010 at 01:49 AM

hey where to get the same star images for my program I am getting only crap images in google search Plz give me the link fo rhe star image

by sudheer on Wednesday, May 5th 2010 at 01:08 PM

Hi, is it possible to receive the results in the email. Tried to embed it with form mail. but no success. any idea?
just want the results in email. can anyone help?

Thank you!
Sudheer
(beginner in development field)

by bettercode on Wednesday, June 23rd 2010 at 10:52 AM

OR... if you want the same code but not total crap, and without the blinking, and such that you can click more than once, here ya go : )

<script type="text/javascript">
var set=false;
var a=0;

function loadStars()
{
star1 = new Image();
star1.src = "star1.gif";
star2 = new Image();
star2.src= "star2.gif";
}

function highlight(x)
{
y=x*1 1;
for (i=1;i<y;i )
{
document.getElementById(i).src= star2.src;
}
for(i=y; i < 6; i ){
document.getElementById(i).src= star1.src;
}
}
function losehighlight(x)
{
highlight(a);
}

function setStar(x)
{
y=x*1 1;
for (i=1;i<6;i )
{
if(i<y)
{
document.getElementById(i).src= star2.src;
}
else
{
document.getElementById(i).src= star1.src;
}
}
a=x*1;
set=true;
}

</script>


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs JavaScript Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons