I have written a code for rating system star rating system basically, it was all working fine and good the code is
<script type="text/javascript">
(function($){
$(document).ready(function() {
var $option = $('.option');
var $fruit = $('.fruit-name');
var $last;
var parent
$option.click(function() {
parent=$(this).parents('.comment-form-rating');
$('.fruit-name',parent).val(this.innerHTML)
})
$(".starr1").hover(function(){
parent=$(this).parents('.comment-form-rating');
$('.starr1',parent).css('color','gold')
},function(){
$(".starr1").css("color","#ddd");
});
$(".starr2").hover(function(){
parent=$(this).parents('.comment-form-rating');
$('.starr1',parent).css('color','gold')
$('.starr2',parent).css('color','gold')
},function(){
$(".starr1").css("color","#ddd");
$(".starr2").css("color","#ddd");
});
$(".starr3").hover(function(){
parent=$(this).parents('.comment-form-rating');
$('.starr1',parent).css('color','gold')
$('.starr2',parent).css('color','gold')
$('.starr3',parent).css('color','gold')
},function(){
$(".starr1").css("color","#ddd");
$(".starr2").css("color","#ddd");
$(".starr3").css("color","#ddd");
});
$(".starr4").hover(function(){
parent=$(this).parents('.comment-form-rating');
$('.starr1',parent).css('color','gold')
$('.starr2',parent).css('color','gold')
$('.starr3',parent).css('color','gold')
$('.starr4',parent).css('color','gold')
},function(){
$(".starr1").css("color","#ddd");
$(".starr2").css("color","#ddd");
$(".starr3").css("color","#ddd");
$(".starr4").css("color","#ddd");
});
$(".starr5").hover(function(){
parent=$(this).parents('.comment-form-rating');
$('.starr1',parent).css('color','gold')
$('.starr2',parent).css('color','gold')
$('.starr3',parent).css('color','gold')
$('.starr4',parent).css('color','gold')
$('.starr5',parent).css('color','gold')
},function(){
$(".starr1").css("color","#ddd");
$(".starr2").css("color","#ddd");
$(".starr3").css("color","#ddd");
$(".starr4").css("color","#ddd");
$(".starr5").css("color","#ddd");
});
});
})(jQuery);
</script>
but then I have to define a click function for my stars that when user click on star 5 make all star red color —
I defined it like this
$(".starr5").click(function () {
parent = $(this).parents('.comment-form-rating');
$('.starr1', parent).css('color', '#FF7777')
$('.starr2', parent).css('color', '#FF7777')
$('.starr3', parent).css('color', '#FF7777')
$('.starr4', parent).css('color', '#FF7777')
$('.starr5', parent).css('color', '#FF7777')
});
Here’s where the Problem occurs – now star become red but on mouse left they become gray again and on mouse over the yellow function of hover starts working – totatly confused How to write further code making it work.
You should just add and remove classes on enter and leave, so when
on
mouseenter
on
click
on
mouseleave
And you are doing just fine; Happy Coding.
Star1 example on request:
I can immagine the mess you have in your HTML with all your elements and classes…
To simplify your life I have written a tremendously simple code:
STARS RATING DEMO
This is all you need:
HTML:
where data-rated is a value (1-5) returned by your server.
Create an image that matches your background color, create 5 star-shaped holes and save as .png
CSS:
jQuery: