Noah Telussa Noah Telussa - 2 years ago 58
Javascript Question

jQuery star rating plugin variable doesnt return correctly

I'm making a rating system to rate a movie using 5 stars. I use the

jQuery bar plugin
for this (jQuery bar plugin).

Someone can vote from 1 to 5. The data is stored in the database. After someone votes the stars will be disabled and show the amount of stars the user rated.

With the
value of the plugin it will define how many stars are 'selected'. So if this variable is 4, 4 stars will be colored.

When I hard code for example
initialRating: 4
it works. Although when I use
initialRating: currentRating
(the voted amount of stars) it won't return the right amount and it will just show 1 star. When I
it will return

So how does it come that hardcoding
works, instead of
(with the value of 4) which doesn't work.


$(function () {

currentRating = $(".rating").val(); //Getting the value that someone vote previously
console.log(currentRating); // Displays 4

$('#rating').barrating('show', {
theme: 'fontawesome-stars',
showSelectedRating: true,
initialRating: currentRating,
onSelect: function (value, text) {
movieTitle = $('.movie-name'). text(),
tmdb_id = $(".imdb_id").val(),
_token = $(".csrf_field").val();

headers: {
'X-CSRF-TOKEN': _token
method: "POST",
url: "/movies/" + tmdb_id + "/rate",
data: {
title: movieTitle,
tmdb_id: tmdb_id,
rating: value
success: function (result) {

enter image description here

Answer Source

The variable currentRating is a string whereas the plug-in is expecting a integer.

Converting the string into an integer using parseInt() will solve the problem.


currentRating = parseInt(currentRating);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download