I need to make grading system in jQuery ( I click on one circle and than that and previous circles become green).
If I click on the empty circle, that circle and previous ones become green.
If I write number of circles in input box and than click on "Update max value" button, than I get the desired number of circles, but if I click afterwards on one of those circles, they don't become green, even though they have a class rating-circle
.
Why is that happening?
I put my code below.
$(function() {
//this function is drawing circles
var makingCircles = function(numberOfStars) {
$("#rating-container").empty();
for (let i = 0; i < numberOfStars; i++)
$('<div class="rating-circle"></div>').appendTo('#rating-container');
}
var chosenGrade = null;
var numberOfStars = $("#rating-container").attr("max-value");
makingCircles(numberOfStars); // this is for initial drawing of circles
$("#update-max-value").on("click", function() {
numberOfStars = $("#max-value").val();
makingCircles(numberOfStars);
});
$(".rating-circle").click( // this function is for rating
function() {
chosenGrade = $(this);
chosenGrade.addClass("rating-chosen");
chosenGrade.prevAll().addClass("rating-chosen");
});
});
body {
font-family: Verdana;
}
h1,
h2,
h3 {
color: darkblue;
}
.rating-circle {
height: 2em;
width: 2em;
border: .1em solid black;
border-radius: 1.1em;
display: inline-block;
margin: 0.3em;
padding: 0.1em;
}
.rating-hover {
background-color: yellow;
}
.rating-chosen {
background-color: green;
}
<h2>Finding elements using jQuery</h2>
<div>This session is about identifying elements using jQuery methods and selectors.</div>
<h3>Rate this session</h3>
<div id="rating-container" max-value="5">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<label for="max-value">Enter max value:</label>
<input type="text" id="max-value" />
<button type="button" id="update-max-value">Update max value</button>
</div>