I'm using this plugin to make a simple star rating system -> http://antenna.io/demo/jquery-bar-rating/examples/
Works pretty good up to a point where I get stuck.. I followed the documentation and I made a div with some select items.
$('#skills').barrating({
theme: 'fontawesome-stars',
initialRating: null,
onSelect: function(value, text, event) {
if (typeof(event) !== 'undefined') {
// rating was selected by a user
val = $(event.target).data("rating-value");
console.log(val);
}
}
});
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-bar-rating/1.2.2/jquery.barrating.min.js"></script>
<select id="skills">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select id="communication">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select id="quality">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
And I'm using the script like so:
$('#skills').barrating({
theme: 'fontawesome-stars',
initialRating: null,
onSelect: function(value, text, event) {
if (typeof(event) !== 'undefined') {
// rating was selected by a user
val = $(event.target).data("rating-value");
console.log(val);
}
}
});
As you can see on the OnSelect function I'm able to get the star rating value that the user selected. The problem is, I want to store this value in a variable outside of the onSelect function and outside of the barrating function.
I want this because I have multiple star ratings and I need to make an average of those ratings. Any suggestions?
val
variable? – Davidval
variable is in a higher scope than that function, possibly on thewindow
object itself. – Davidval
withvar
(or something similar likeconst
orlet
),val
is attached to thewindow
object. Usingstrict mode
, this would through an error. – Raphael Rafatpanah