jquery function inside while loop

0

I'm using a library called Jquery Bar Rating for the use of rate with 5 stars, I have comments saved in a table with a rate of 1 to 5 stars each, and those comments I printed them with a while:

while($row = mysqli_fetch_assoc($res)):
    echo '<blockquote>
            <select class="seeRate">//Select para mostrar estrellas
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3" selected>3</option>
              <option value="4">4</option>
              <option value="5">5</option>
            </select>
        <p class="text-justify" style="font-size: 14px;">'.ucfirst($row['note']).'</p>
        <small class="pull-right"><cite><i>'.ucwords(getNameEmp($row['noteBy'])).' - '.$row['noteAt'].'</i></cite></small>';
        echo '</blockquote>';
        echo '<script>$(\'.seeRate\').barrating(\'show\');</script>';
endwhile;

And this is the trigger for the stars:

$('.seeRate').barrating({
    theme: 'bootstrap-stars',
    readonly: false
  });
});

at the moment of putting the stars inside the while, they only show up well in the first comment, in the others only the dropdown without style is shown, as if the library could not be found.

    
asked by Fernando Garcia 19.12.2018 в 00:00
source

1 answer

0

First, you have to get the echo '<script>$(\'.seeRate\').barrating(\'show\');</script>'; of the while loop, because it will generate the same script label as many times as there are results.

On the other hand, you must wait for the document to finish loading with document.ready ()

$(document).ready(function(){
  $('.seeRate').barrating({
    theme: 'bootstrap-stars',
    readonly: false
  });
  $('.seeRate').barrating('show');
});
    
answered by 19.12.2018 в 09:46