calling JavaScript function inside foreach loop

Ben

I have rating-stars, using foreach need to addClass depends on the value been passed from the model. i.e. if 2 (then first 2 stars class rated is applied)

 foreach (var rev in Model.Reviews)
 {
   <p class="rating">  
   <span id="5" class="fa fa-star"></span>
   <span id="4" class="fa fa-star"></span>
   <span id="3" class="fa fa-star"></span>
   <span id="2" class="fa fa-star"></span>
   <span id="1" class="fa fa-star"></span>
   </p>

   <script>
       apply(@rev.Rating);
   </script>
 }

apply function:

 <script type="text/javascript">
           function apply(r) {
               $('.rating span').each(function () {
                    if (this.id <= r) {
                        $(this).addClass("rated");
                    }
                });
            }
  </script>

The problem for example: when rev.Rating is 2, the addClass is applied correctly to two ids. But then when next value is 4 it overrides the first and it applies addClass to 4 stars and then next is 4 stars.

the issue it overrides the previous stars with the current model value.

I think because the p class="rating" is the same. But I thought if I pass (this) current reference it shouldt override the other elements. Any ideas why? and How I can fix it.

using this code inside a view, using MVC

Arun P Johny

Yes the problem is your use of apply method, you can have a single dom ready callback which will update the rated class like below.

In each rating element, you can store its initial value using a data-* value, which can be used in a dom ready handler after the loop.

Also since ID of an element must be unique, you can use another data-* attribute to store the value of the span

jQuery(function($) {
  $('.rating').each(function() {
    $(this).find('span').slice(-$(this).data('default-value')).addClass("rated");
  });
})
.rated {
  border: 1px solid red;
}
<p class="rating" data-default-value="2">
  <!-- data-default-value="@rev.Rating" -->
  <span data-value="5" class="fa fa-star"></span>
  <span data-value="4" class="fa fa-star"></span>
  <span data-value="3" class="fa fa-star"></span>
  <span data-value="2" class="fa fa-star"></span>
  <span data-value="1" class="fa fa-star"></span>
</p>
<p class="rating" data-default-value="4">
  <span data-value="5" class="fa fa-star"></span>
  <span data-value="4" class="fa fa-star"></span>
  <span data-value="3" class="fa fa-star"></span>
  <span data-value="2" class="fa fa-star"></span>
  <span data-value="1" class="fa fa-star"></span>
</p>


So

foreach (var rev in Model.Reviews)
 {
   <p class="rating" data-default-value="@rev.Rating"> 
       <span id="5" class="fa fa-star"></span>
       <span id="4" class="fa fa-star"></span>
       <span id="3" class="fa fa-star"></span>
       <span id="2" class="fa fa-star"></span>
       <span id="1" class="fa fa-star"></span>
   </p>
 }
<script>
    jQuery(function ($) {
        $('.rating').each(function () {
            $(this).find('span').slice(-$(this).data('default-value')).addClass("rated");
        });
    })
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a Function Inside a Loop

From Dev

Unclear about returning from javascript forEach loop inside a function

From Dev

calling an api inside a foreach loop in angularjs

From Dev

JavaScript Function inside the loop

From Dev

for loop inside for loop function in javascript

From Dev

for loop inside for loop function in javascript

From Dev

Calling a function after a specific time, inside a loop

From Dev

Calling a function after a specific time, inside a loop

From Dev

Javascript calling function from inside same function

From Dev

Javascript calling function from inside same function

From Dev

Calling a function inside another function in JavaScript

From Dev

Javascript: Calling a function inside another function

From Dev

Foreach loop inside function with sets of two variables

From Dev

Break setTimeout function inside foreach loop

From Dev

PHP copy function not working inside foreach loop

From Dev

how to return value inside of forEach loop in JavaScript

From Dev

Get index inside javascript foreach loop

From Dev

If statement not working inside foreach loop javascript

From Dev

Javascript recursive function inside a for loop

From Dev

handle javascript function inside loop

From Dev

issue calling a javascript function inside razor if statement

From Dev

Calling a JavaScript variable inside a function from outside

From Dev

Calling a JavaScript function inside PHP Array

From Dev

JavaScript function inside echo in PHP script is not calling

From Dev

Knockout JS Calling a ViewModel function inside a foreach binding

From Dev

Creating a separate function forEach loop? Javascript

From Dev

Scope issue calling a callback function inside a loop in node.js

From Dev

Calling a function every 40 second inside a while loop

From Dev

Calling programs inside a loop

Related Related

  1. 1

    Calling a Function Inside a Loop

  2. 2

    Unclear about returning from javascript forEach loop inside a function

  3. 3

    calling an api inside a foreach loop in angularjs

  4. 4

    JavaScript Function inside the loop

  5. 5

    for loop inside for loop function in javascript

  6. 6

    for loop inside for loop function in javascript

  7. 7

    Calling a function after a specific time, inside a loop

  8. 8

    Calling a function after a specific time, inside a loop

  9. 9

    Javascript calling function from inside same function

  10. 10

    Javascript calling function from inside same function

  11. 11

    Calling a function inside another function in JavaScript

  12. 12

    Javascript: Calling a function inside another function

  13. 13

    Foreach loop inside function with sets of two variables

  14. 14

    Break setTimeout function inside foreach loop

  15. 15

    PHP copy function not working inside foreach loop

  16. 16

    how to return value inside of forEach loop in JavaScript

  17. 17

    Get index inside javascript foreach loop

  18. 18

    If statement not working inside foreach loop javascript

  19. 19

    Javascript recursive function inside a for loop

  20. 20

    handle javascript function inside loop

  21. 21

    issue calling a javascript function inside razor if statement

  22. 22

    Calling a JavaScript variable inside a function from outside

  23. 23

    Calling a JavaScript function inside PHP Array

  24. 24

    JavaScript function inside echo in PHP script is not calling

  25. 25

    Knockout JS Calling a ViewModel function inside a foreach binding

  26. 26

    Creating a separate function forEach loop? Javascript

  27. 27

    Scope issue calling a callback function inside a loop in node.js

  28. 28

    Calling a function every 40 second inside a while loop

  29. 29

    Calling programs inside a loop

HotTag

Archive