Skip to content

Instantly share code, notes, and snippets.

@robertdevore
Created March 1, 2019 19:36
Show Gist options
  • Select an option

  • Save robertdevore/d4b449e95a2ce9c38a95a9e2fd424cfb to your computer and use it in GitHub Desktop.

Select an option

Save robertdevore/d4b449e95a2ce9c38a95a9e2fd424cfb 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',
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