Star Rating Save Data

I am using Barbatus:stars-rating package and I want to save the user input for how many star are clicked.

I have this in my main.js - client

 Template.images.events({
	'click .js-rate-image':function() {
		var rating = $(this).data("userrating");
		console.log(rating);
       };
});

and this is my html code

<p>{{>starsRating mutable=true class="js-rate-image" id=_id}}</p>

the console says undefined. What’s going on?

Try setting the id to js-rate-image instead of using a class attribute and see what happens. Also change your code to:

Template.images.events({
	'click #js-rate-image':function() {
        var star = document.getElementById('js-rate-image');
        console.log("star.dataset.userrating");
		var rating = star.dataset.userrating; // Gives you the value of the star selected for this instance
		console.log(rating);
       };
});

This should work :slight_smile:

'click .js-rate-image':function() {
	var rating = $(this).data("userrating");
	console.log(rating);
};

Should be

'click .js-rate-image':function(e) {
	var rating = $(e.currentTarget).data("userrating");
	console.log(rating);
}