antennaio / jquery-bar-rating

jQuery Bar Rating Plugin - minimal, light-weight jQuery ratings.
http://antennaio.github.io/jquery-bar-rating/
MIT License
736 stars 263 forks source link

onSelect return empty value if the rating was set programmatically #116

Open Hamime opened 5 years ago

Hamime commented 5 years ago

Hello there, I had an issue with the plugin when using the onSelect Callback when i set the rating using the set method or by using the initialRating ! The solution took some while but was easy to solve. I would like to share it with you when someone face the same issue ! I hope this issue get solved in future updates ! The walkaround solution is :

$("#example").barrating("show", {
    theme: "my-awesome-theme",
    onSelect: function (value, text, event) {
        if (typeof (event) !== "undefined") {
            // rating was selected by a user
            console.log(event.target);
            //Get the selected value from the event
            var selectedValue = $(event.target).data("rating-value");
            console.log("Selected value :" + selectedValue);
        } else {
            // rating was selected programmatically
            // by calling "set" method
        }
    }
});

just use $(event.target).data('rating-value') and you will get the selected value even if it was set programmatically and only the onSelect returns an empty value.

Best of luck :)