I'm working on rating stars. My question is how to check stars dynamically for example if we pass a variable with value 4.2
then dynamically 4.2
stars four an half
star has to checked.
How I can achieve this using JQuery? This is my HTML:
Thanks.
$(document).ready(function(){
var value = 3;
$("input[name=rating][value=" + value + "]").attr('checked', 'checked');
});
.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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>