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>