Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Star rating system save value on click

I'm trying to make a star rating system, i have this code and i want to make some changes on it, after the user clicks on the stars it shows an alert with how many stars and it resets the colors, what i want is the color filling to stay after the user clicks on it, and replace the alert with a div under the stars, here is my code:

$(function() {
    $("div.star-rating > s, div.star-rating-rtl > s").on("click", function(e) {
        var numStars = $(e.target).parentsUntil("div").length+1;
        alert(numStars + (numStars == 1 ? " star" : " stars!"));
    });
});
.star-rating s:hover {
    color: red;
}
.star-rating-rtl s:hover {
    color: red;
}

.star-rating s,
.star-rating-rtl s {
    color: black;
    font-size: 50px;
    cursor: default;
    text-decoration: none;
    line-height: 50px;
}
.star-rating {
    padding: 2px;
}
.star-rating-rtl {
    background: #555;
    display: inline-block;
    border: 2px solid #444;
}
.star-rating-rtl s {
    color: yellow;
}
.star-rating s:hover:before, .star-rating s.rated:before {
    content: "\2605";
}
.star-rating s:before {
    content: "\2606";
}
.star-rating-rtl s:hover:after, .star-rating-rtl s.rated:after{
    content: "\2605";
}
.star-rating-rtl s:after {
    content: "\2606";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="star-rating"><s><s><s><s><s></s></s></s></s></s></div>
<div class="star-rating-rtl"><s><s><s><s><s></s></s></s></s></s></div>
like image 736
jessica Avatar asked Jun 09 '17 16:06

jessica


People also ask

How star ratings are calculated?

In general, the star rating is simply the mean score divided by 20, to get a star rating on a 0-5 scale.

What does star rating system indicate?

Star classification is a type of rating scale utilizing a star glyph or similar typographical symbol. It is used by reviewers for ranking things such as films, TV shows, restaurants, and hotels. For example, a system of one to five stars is commonly used in hotel ratings, with five stars being the highest rating.

How do you make a dynamic star rating?

Go to the settings panel on the left side. Click the dynamic icon on the Rating field under the Rating block and select ACF Number Field. Click the wrench icon on the ACF Number Field field and select the key (custom field) you have created on step one above. You can add a prefix on the Title field.

How do you use a 5 star rating system?

5-star calculations are a simple average— add all of your individual scores, divide by the number of individual responses, and there you have it—your average 5-star rating. The 5-star score is rounded to the nearest tenth.


1 Answers

See for the .active class added in CSS to find the changes there.

See code and comments in JS

$(function() {
    $("div.star-rating > s, div.star-rating-rtl > s").on("click", function(e) {
    
    // remove all active classes first, needed if user clicks multiple times
    $(this).closest('div').find('.active').removeClass('active');

    $(e.target).parentsUntil("div").addClass('active'); // all elements up from the clicked one excluding self
    $(e.target).addClass('active');  // the element user has clicked on


        var numStars = $(e.target).parentsUntil("div").length+1;
        $('.show-result').text(numStars + (numStars == 1 ? " star" : " stars!"));
    });
});
.show-result {
  margin: 10px;
  padding: 10px;
  color: green;
  font-size: 20px;
}

.star-rating s:hover,
.star-rating s.active {
    color: red;
}
.star-rating-rtl s:hover,
.star-rating-rtl s.active {
    color: red;
}

.star-rating s,
.star-rating-rtl s {
    color: black;
    font-size: 50px;
    cursor: default;
    text-decoration: none;
    line-height: 50px;
}
.star-rating {
    padding: 2px;
}
.star-rating-rtl {
    background: #555;
    display: inline-block;
    border: 2px solid #444;
}
.star-rating-rtl s {
    color: yellow;
}
.star-rating s:hover:before,
.star-rating s.rated:before,
.star-rating s.active:before {
    content: "\2605";
}
.star-rating s:before {
    content: "\2606";
}
.star-rating-rtl s:hover:after,
.star-rating-rtl s.rated:after,
.star-rating-rtl s.active:after {
    content: "\2605";
}

.star-rating-rtl s:after {
    content: "\2606";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="star-rating"><s><s><s><s><s></s></s></s></s></s></div>
<div class="star-rating-rtl"><s><s><s><s><s></s></s></s></s></s></div>

<div class="show-result">No stars selected yet.</div>
like image 63
caramba Avatar answered Oct 03 '22 09:10

caramba