2

I need to loop through a data array and set value for each div element, In the line

$(".rate-circle").data('value', value);

Presently I'm doing this using the code below, however with this approach the value set for my elements is the last element i.e. 35.

 var val = [55, 70, 88, 35];//, function (i, l) {
            $("div").each(function () {
                $.each(val, function (index, value) {
                    $(".rate-circle").data('value', value);
                    $(".rate-circle").rateCircle({
                        size: $("#rate-circle-size").val(),
                        lineWidth: 4,
                        fontSize: $("#rate-circle-font-size").val(),
                    });

                });
            });

Suggest where am I going wrong, I need to loop through the array and get the values and to set value corresponding to each elements

4
  • 1
    .data() will set data for all the matched elements...jsfiddle.net/rayon_1990/z2wtajud Commented Mar 28, 2016 at 7:23
  • What @rayon-dabre said. Every time you use .data() in your inner loop it sets every div.rate-circle 'data-value' attribute to the current value. You might want to give more details of your problem, like an example of the HTML in your page. Commented Mar 28, 2016 at 7:28
  • 1
    jsfiddle.net/arunpjohny/LoLLp4jc/1 Commented Mar 28, 2016 at 7:29
  • @ArunPJohny : This solved.. Please include this as a solution, so that I can mark as answer Commented Mar 28, 2016 at 8:20

1 Answer 1

1

As already explained in the comments, $(".rate-circle").data() will set the same value to all rate-circle elements, instead you need to do an index based assignment

$(".rate-circle").each(function(idx) {
  $(this).data('value', val[idx]);
}).rateCircle({
  size: $("#rate-circle-size").val(),
  lineWidth: 4,
  fontSize: $("#rate-circle-font-size").val(),
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.