Search code examples
jqueryjavascriptsparklines

jQuery sparklines iterate over div


I have an array of divs and I want to iterate over each div and pass it to sparklines. like so:

var divs = $("#wrapper>div");
var data = ["1:2:3:4", "4:3:2:3:1"];
var i;

for(i = 0; i<data.length; i++){
   divs[i].sparkline(data[i]);
}

I get the error "divs[i].sparkline" is not a function. But if I do this

divs.sparkline([1,2,3,4]);

its fine all the divs get the linegraph but with the same data.

Does anyone have any suggestions but how to use sparkline when iterating over divs?

Thanks!


Solution

  • Try changing divs[i] to $(divs[i])

    for(i = 0; i<data.length; i++){
       $(divs[i]).sparkline(data[i]);
    }
    

    the sparkline works on jQuery object and divs[i] would be dom node. So try wrapping it inside $(..).