javascript - Unable to get $.each to work in JQuery -


a working example without $.each

failed example $.each

i'm using a pie chart plugin gets data data attributes of li render pie. ran problem when tried running script in $.each function. looks fails data data attributes of each container. can figure out how solve that?

function chart(chartid,stats) {   new chart.pie(chartid, {     showtooltips: true,     chartminsize: [200, 200],     chartmaxsize: [250, 250],     chartdata: stats   }); }  $('.p_pie').each(function(){   var chartid = $(this).attr('id'),       arr = [],       stats = $(this).find('.stats_area li:not(:first)').map(function() {     return [$(this).data('value').split(',')];   }).get();   chart(chartid,stats);   console.log(json.stringify(stats)); }); 

html:

<div id='mychart' class='fl p_pie'></div> <ul class='stats_area legend fl shr pieid'>   <li>gender</li>   <li class='pie_0' data-value='male,2,male,#6495ed'><em>male</em><span> 2</span></li>   <li class='pie_1' data-value='female,5,female,#dc143c'><em>female</em><span> 5</span></li> </ul>   <div id='mychart2' class='fl p_pie'></div>  <ul class='stats_area legend fl shr pieid'>    <li>age</li>    <li class='pie_0' data-value='1-10,12,1-10,#6495ed'><em>1-10</em><span> 12</span></li>    <li class='pie_1' data-value='11-20,25,11-20,#dc143c'><em>11-20</em><span> 25</span></li>  </ul> 

your .p_pie divs don't contain anything. ;)

here jsfiddle showing .each() working fine.

in code gave, uls aren't inside divs, each() doesn't find elements iterate over!

now keep in mind in code, select id of current element create pie chart in. means whatever content have in element overwritten, can see in jsfiddle above.

to avoid this, create new div contain chart id want, , id of element make chart in data-id attribute of div contains data.

here example of that.

$('.p_pie').each(function () {     var chartid = $(this).data('id'), // data-id attr of div         stats = $(this).find('.stats_area li:not(:first)').map(function () {             return [$(this).data('value').split(',')];         }).get();     chart(chartid, stats); }); 

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -