Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply raty plugin to new generated divs?

Tags:

jquery

raty


I have a page with raty divs. With the classic code $('.raty').raty({...}); the plugin works perfectly on these existing divs. But when I load new raty divs thanks to an ajax function, new divs are "not transformed into stars". Could you please help me to find my mistake ?

$.ajax({
    url: '/ws/player/reviews/p/1',
    context: document.body,
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    jsonp: "callback",
    jsonpCallback: "jsonpCallbackfunction",
    success: function(data) {
        $.each(data.response.reviews, function( key, value ) {
            html = '';
            html += '<div class="raty read" data-rating="5"></div>';
            $('#reviews').append(html);
        });

    }
    data: {player_id: player_id, from: $('#reviews').data('from')}
}).done(function() {
    $(this).find('.raty').raty({
        path: '/img/raty/',
        readOnly: true,
        score: function() {return $(this).data('rating');}
    });
});

Although, if I try $(this).find('.raty').html('blablabla'); "blablabla" is correctly written in all my '.raty' divs.

Thank you for you help,

Jeremy

like image 886
Jeremy Avatar asked Feb 27 '13 08:02

Jeremy


1 Answers

you have done little mistake

see this code

$.ajax({
url: '/ws/player/reviews/p/1',
context: document.body,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
jsonp: "callback",
jsonpCallback: "jsonpCallbackfunction",
success: function(data) {
    $.each(data.response.reviews, function( key, value ) {
        html = '';
        html += '<div class="raty read" data-rating="5"></div>';
        $('#reviews').append(html);
    });
}
data: {player_id: player_id, from: $('#reviews').data('from')}}).done(function() {
$(this).find('#reviews .raty').raty({ // Changes
    path: '/img/raty/',
    readOnly: true,
    score: function() {return $(this).data('rating');}
});});

Hope, this code will work

like image 198
Vikram Avatar answered Nov 06 '22 23:11

Vikram