jquery abstractly adding up values of parent div's data values -
so have several divs class "answers_total", , contained in 5 divs class "answer" , data-answer attribute.
what i'm trying somehow abstractly add total true values each "answers_total" div , store them in variable. first 1 here there 3 "true" , in second 2 "true". i'd need able access each individual total, each have own variable? i'm not 100 percent sure.
i think you'd $(".answers_total").each...
but i'm not sure go after that. ideas on how this? maybe it's not possible or there better way set , this?
thanks help
<div class="answers_total"> <div data-answer="true" class="answer">speech</div> <div data-answer="false" class="answer">religion , belief</div> <div data-answer="true" class="answer">press</div> <div data-answer="true" class="answer">assembly</div> <div data-answer="false" class="answer">petition</div> </div> <div class="answers_total"> <div data-answer="false" class="answer">speech</div> <div data-answer="true" class="answer">religion , belief</div> <div data-answer="false" class="answer">press</div> <div data-answer="true" class="answer">assembly</div> <div data-answer="false" class="answer">petition</div> </div>
you can use jquery.map() function returns array.. use .length
property find out how many of data-answer=true
exists in each div.answers_total
var x = $('.answers_total').map(function(i,v){ return $('div[data-answer=true]',v).length; // return how many have data-answer=true });
so
x[0] = 3 // relevant first .answers_total div x[1] = 2 // relevant second .answers_total div
if wanted count of both true , false count can
var x = $('.answers_total').map(function (i, v) { return { true: $('div[data-answer=true]', v).length, false: $('div[data-answer=false]', v).length }; });
then
x[0].true = 3 // number of true in first div x[0].false = 2 // number of false in first div x[1].true = 2 // number of true in second div x[1].false= 3 // number of false in second div
Comments
Post a Comment