I’m displaying feedback and rating for a particular entity which is shown as tab initially. When the user clicks a tab, I am getting feedback and rating using
$.get()
The return of $.get() is something like this
some arbit html|||8
This data I’m splitting using |||and getting the rating which is 8 in this case.
$.get('prevfeed.php',{text:text},function(data){
data = data.split("|||");
$('#prevFeedback').html(data[0]);
$('body').data( 'rating', parseInt(data[1]));
alert(parseInt(data[1])+1);
});
The problem is here. I am using this plugin to show star rating. The following is the initializing code
$('#star').raty({
readOnly: true,
number : 10,
start: $('body').data( 'rating')
});
Whatever i do The rating does not work. If i put a number instead of $('body').data( 'rating'), it’s working fine.
Initially I tried setting a value of hidden input field using $('body').data( 'rating')and then assigning start with the val of the input field. But it’s of no use.
Thanks in advance
P.S: Seeing the answers I edited my code
$('#star').raty({
readOnly: true,
number: 10,
start: function() { alert($('body').data('rating'););return $('body').data('rating'); }
When I include alert, it is working. So it is like I just have to deplay this code. How to do this.
});
This is because the code where you set the
ratingattribute on thebodyis delayed until the AJAX call has completed.However, you’ll be initiating the
ratyplugin on page load; where theratingdata attribute is stillundefined.You’ll have to delay the initialization of the plugin until the AJAX call has completed: