Skip to content

Instantly share code, notes, and snippets.

@alexmangini
Forked from robertdevore/star-rating.js
Created March 1, 2019 19:56
Show Gist options
  • Save alexmangini/28719a8c8e100d931fe4ac4a395e43fa to your computer and use it in GitHub Desktop.
Save alexmangini/28719a8c8e100d931fe4ac4a395e43fa to your computer and use it in GitHub Desktop.
Star rating script code example.
$(function() {
$('.driver-rating').barrating({
theme: 'fontawesome-stars',
initialRating: "<?php echo $averageRating; ?>",
showSelectedRating: true,
// onSelect is what triggers the saving of the rating.
onSelect: function(value, text, event) {
// Get element id by data-id attribute
var el = this;
var el_id = el.$elem.data('id');
// rating was selected by a user
if (typeof(event) !== 'undefined') {
var split_id = el_id.split("_");
var postid = split_id[1]; // postid.
alert(postid + ' ' + value);
// AJAX Request
$.ajax({
url: "<?php echo $ajax_url; ?>",
type: 'post',
action: 'my_function_name',
data: {postid:postid,rating:value},
dataType: 'json',
success: function(data) {
// Update rating.
var therating = data['rating'];
$('#avgrating_'+postid).text(therating);
}
});
}
} // end onSelect
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment