0

When user hovers on a circle, that particular circle along with the circles to its left should get filled with yellow and when user clicks on a circle, that particular circle along with the circles to its left should get filled with green.

When user hovers again, last active circles should rollback to hover effect function and get filled with yellow, and if the user doesn't like to click again, the previously filled circle with green must be retained.

I've given !important for the rating-hover class to give it a priority when user hovers again, now the problem is when user rates 4 for the first time and again thinks to rate 2 after that and hovers on second circle the previously filled 4 stars are still seen, I don't want to see that, the user must feel like rating newly when he hover again, if left unclicked the previously clicked stars must be retained. And please I don't like to use !important; any other solutions are welcome!

$(function() {
  $('.rating-circle').hover(function() {
      $(this).prevAll().addBack().addClass('rating-hover');
    },
    function() {
      $(this).prevAll().addBack().removeClass('rating-hover');
    });
});

$(function() {
  $('.rating-circle').click(function() {
    $('.rating-circle').removeClass('rating-chosen');
    $(this).prevAll().addBack().addClass('rating-chosen');
  });
});
.rating-circle {
  height: 2em;
  width: 2em;
  border: .1em solid black;
  border-radius: 1.1em;
  display: inline-block;
  margin: 0;
  padding: .1em;
}
.rating-hover {
  background-color: yellow !important;
}
.rating-chosen {
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="rating-container">
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
</div>

2 Answers 2

1

To achieve the effect you require you just need to set the .rating-hover class to be more specific that .rating-chosen so that it overrides. To do this I prefixed it with an element selector, specifically div. You then need to also remove the .rating-hover class when the click event fires.

Finally, note that you can also put all the code in a single document.ready handler. Try this:

$(function() {
  $('.rating-circle').hover(function() {
    $(this).prevAll().addBack().addClass('rating-hover');
  }, function() {
    $(this).prevAll().addBack().removeClass('rating-hover');
  });

  $('.rating-circle').click(function() {
    $('.rating-circle').removeClass('rating-chosen rating-hover');
    $(this).prevAll().addBack().addClass('rating-chosen');
  });
});
.rating-circle {
  height: 2em;
  width: 2em;
  border: .1em solid black;
  border-radius: 1.1em;
  display: inline-block;
  margin: 0;
  padding: .1em;
}
div.rating-hover {
  background-color: yellow;
}
.rating-chosen {
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="rating-container">
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, i have already achieved that. Let's suppose user rates 4 for the first time, and again thinks of rating 2 stars, so now previously filled 4 star remains when user tries to hover on second star. I mean to say when user hovers again the previously filled stars should not be seen.
1

Try adding !important to the rating-hover class. The hover is working correctly but because of the rating-chosen last in your style, you will need the !important there.

$(function() {
  $('.rating-circle').hover(function() {
      $(this).prevAll().addBack().addClass('rating-hover');
    },
    function() {
      $(this).prevAll().addBack().removeClass('rating-hover');
    });
});

$(function() {
  $('.rating-circle').click(function() {
    $('.rating-circle').removeClass('rating-chosen');
    $(this).prevAll().addBack().addClass('rating-chosen');
  });
});
.rating-circle {
  height: 2em;
  width: 2em;
  border: .1em solid black;
  border-radius: 1.1em;
  display: inline-block;
  margin: 0;
  padding: .1em;
}
.rating-hover {
  background-color: yellow !important;
}
.rating-chosen {
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="rating-container">
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
</div>

1 Comment

Thank you, i have got that. Let's suppose user rates 4 for the first time, and again thinks of rating 2 stars, so now previously filled 4 star remains when user tries to hover on second star. I mean to say when user hovers again the previously filled stars should not be seen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.