<!--language: lang-html-->
@foreach (var book in Model.Category.Books)
{
<div class="rate-star-@book.Id"></div>
<script>
$(".rate-star-@book.Id").stars({ stars:5, readonly:false});
</script>
}
A brief explanation about what I'm trying to do: This script code inside the loop is one of the star rating plugins that I need to implement for books on the page. The problem here is that I need to include jquery code further up in the body but since JQuery library loads later than the script code, I get "$ is not defined" error.
What Have I tried:
I tried to implement the solution on this page
[Handling code which relies on jQuery before jQuery is loaded written by Ashkan Mobayen Khiabani. I put the code inside a function. The function is called at the bottom of the body. It is supposed to be called as many times as it's created in per iteration. But since it's called only once at the bottom the other created functions don't run and only one book gets affected by the function as a result.
<!--language: lang-html-->
@foreach (var book in Model.Category.Books)
{
<div class="rate-star-@book.Id"></div>
<script>
function rateStar{
$(".rate-star-@book.Id").stars({ stars:5, readonly:false});
</script>
}
}
@section scripts
{
<script>
rateStar();
</script>
}
So how to do that function gets called as many times as it's created in the document?