//initialize clicked to false and initializes selected and arrays
var clicked = false;
var selected;
var idArray = [one, two, three, four, five];
//sets the rating stars
function f_setRating(x) {
var i;
switch (x) {
case 'one':
i = 1;
break;
case 'two':
i = 2;
break;
case 'three':
i = 3;
break;
case 'four':
i = 4;
break;
case 'five':
i = 5;
break;
}
//sets the rating number
number.innerHTML = i;
//sets the hovered or clicked star to hover class
idArray[i - 1].className = 'str-hover';
//sets class of all stars below the selected or hovered to str-on
for (pos = 0; pos < (i - 1); pos++) {
idArray[pos].className = 'str-on';
}
//sets class of all stars above the selected or hovered to str-off
for (endPos = 5; endPos > i; endPos--) {
idArray[endPos - 1].className = 'str-off';
}
}
//sets the selected rating
function f_setSelected(x) {
switch (x) {
case 'one':
selected = 0;
break;
case 'two':
selected = 1;
break;
case 'three':
selected = 2;
break;
case 'four':
selected = 3;
break;
case 'five':
selected = 4;
break;
}
}
//sets class of all stars to str-off
function f_clearRating() {
number.innerHTML = '';
for (pos = 4; pos >= 0; pos--) {
idArray[pos].className = 'str-off';
}
}
//resets the rating to the clicked value
function f_resetRating() {
var idArray = ['one', 'two', 'three', 'four', 'five'];
f_setRating(idArray[selected]);
}
//handles onClick event
function f_onClick(x) {
clicked = true;
f_setRating(x);
f_setSelected(x);
}
//handles onMouseOut event
function f_onMouseOut() {
if (clicked) {
f_resetRating();
}
else {
f_clearRating();
}
}
//mouseOut event listener
$(".rating a").mouseout(function() {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.