Rating Star

https://stackoverflow.com/questions/47467536/rating-star-widgets-how-to-checked-starts-dynamically

by Patrick Hund

HTML

<script src="https://use.fontawesome.com/a2e210f715.js"></script>
<div class="rating_widgets">
	<fieldset class="rating">
		<input type="radio" id="star5" name="rating"/>
		<label class = "full" for="star5" title="Awesome - 5 stars"></label>
		<input type="radio" id="star4half" name="rating" value="4 and a half" />
		<label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
		<input type="radio" id="star4" name="rating" value="4" />
		<label class = "full" for="star4" title="Pretty good - 4 stars"></label>
		<input type="radio" id="star3half" name="rating" value="3 and a half" />
		<label class="half" for="star3half" title="Good - 3.5 stars"></label>
		<input type="radio" id="star3" name="rating" value="3" />
		<label class = "full" for="star3" title="Good - 3 stars"></label>
		<input type="radio" id="star2half" name="rating" value="2 and a half" />
		<label class="half" for="star2half" title="ok - 2.5 stars"></label>
		<input type="radio" id="star2" name="rating" value="2" />
		<label class = "full" for="star2" title="ok - 2 stars"></label>
		<input type="radio" id="star1half" name="rating" value="1 and a half" />
		<label class="half" for="star1half" title="Bad - 1.5 stars"></label>
		<input type="radio" id="star1" name="rating" value="1" />
		<label class = "full" for="star1" title="Very bad - 1 star"></label>
		<input type="radio" id="starhalf" name="rating" value="half" />
		<label class="half" for="starhalf" title="Very bad - 0.5 stars"></label>
 </fieldset>
  </div><!-- .rating_widget-->
  <p class="figcaption">4.2 Stars-Based on 1540 Users Reviews</p>

CSS

.rating_widgets {
    display: flex;
    margin-top: 25px;
}

.rating {
    border: none;
    float: left;
}

.rating > input {
    display: none;
}

.rating > label:before {
    margin: 5px;
    font-size: 24px;
    font-family: FontAwesome;
    display: inline-block;
    content: "\f005";
}

.rating > .half:before {
    content: "\f089";
    position: absolute;
}

.rating > label {
    color: #ddd;
    float: right;
}

.rating > input:checked ~ label,
.rating:not(:checked),
.rating:not(:checked) {
    color: #FFD700;
}

.rating > input:checked,
.rating > input:checked,
.rating > input:checked ~ label,
.rating > input:checked ~ label {
    color: #FFED85;
}

JavaScript

var starMap = {
	'0.5': 'half',
    '1': '1',
    '1.5': '1 and a half',
    '2': '2',
    '2.5': '2 and a half',
    '3': '3',
    '3.5': '3 and a half',
    '4': '4',
    '4.5': '4 and a half',
    '5': '5'
}

function roundToHalf(value) {
	return Math.round(value*2)/2;
}

function convertToString(value) {
	return Number(value).toString();
}

function setStarRating(value) {
    var fieldValue = starMap[convertToString(roundToHalf(value))];
    $("input[name=rating][value='" + fieldValue + "']").attr('checked', 'checked');
}

$(document).ready(function(){
    var value = 4.4;
    setStarRating(value);
});