I am working on a rating system where a user can rate by stars.
I made my rating stars as follows:
<div id="rating-submit">
<ol class="rating-stars">
<div class="rating"></div>
<li rate="1"></li>
<li rate="2"></li>
<li rate="3"></li>
<li rate="4"></li>
<li rate="5"></li>
</ol><!--End rating-stars-->
</div><!--End rating-submit-->
I placed <li>
items (stars) over my "rating" <div>
, and if you click on one of the items I style my "rating" <div>
with a CSS background so it looks like you have clicked this star.
I style it by giving the "rating" <div>
an extra class called rating-(1, 2, 3, 4, 5).
jQuery:
jQuery( document ).ready( function( $ ) {
$( '#rating-submit .rating-stars li' ).click( function() {
var rating = $( this ).attr( 'rate' );
$( '#rating-submit .rating' ).addClass( 'rating-' + rating );
});
});
My problem:
If I add for example "rate-3" as class and then click on the second star, the "rate-3" class stays there and it overrules the other class.
My question:
How can I remove the previously added classes if I click on a new star?
Here is my Jsfiddle