Star rating system – Issue with hover + click

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 —

Read More

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.

Related posts

Leave a Reply

2 comments

  1. You should just add and remove classes on enter and leave, so when

    on mouseenter

    .addClass('gold') 
    

    on click

    .removeClass('gold').addClass('red');
    

    on mouseleave

    .removeClass('gold');
    

    And you are doing just fine; Happy Coding.

    Star1 example on request:

       <style type="text/css">
            .gold{color: gold;}
            .red{color: #FF7777;}
            .yourDefaultStarClass{color: #ddd;}
        </style>
    
        <script type="text/javascript">
        (function($){
            $(".starr1").hover(function(){
            parent=$(this).parents('.comment-form-rating');
            $('.starr1',parent).addClass('gold');
            },function(){
            $(".starr1").removeClass('gold');
    
            });
    
            $(".starr5").click(function () {
            parent = $(this).parents('.comment-form-rating');
            $('.starr1', parent).removeClass('gold').addClass('red');
            }); 
    
        });          
        </script>
    
  2. 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:

    <img class="stars" data-rated="4" src="whiteImgWithStarsHoles.png" />
    

    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:

    .stars{
      cursor:pointer;
      max-width:200px; /* respective to the gray image width */
      background: gold url(grayimage200x100.jpg) no-repeat 0 bottom;
      /* transition: 1s;*/
    }
    .stars.red{
      background-color: red;
    }
    

    jQuery:

    $('.stars').each(function(){
      
      var $this = $(this),
          starW = $this.width() / 5,
          rated = 0;
      
      function setRates(){
        rated = $this.data('rated') * starW;
        $this.css({backgroundPosition: rated+'px top'});
      }
      setRates();
      
      $this.on('mousemove', function( e ){
        rated = Math.ceil( (e.clientX-$this.offset().left) / starW );   
        $this.not('.red').css({backgroundPosition: rated*starW +'px top'});
      }).on('mouseleave', setRates).on('click', function(){
        $this.not('.red').data('rated', rated).addClass('red');
        // alert("Send to server: "+   rated   );
      }); 
      
    });