Thursday, 15 August 2013

jquery writing one click statement for query.map set up instead of 3

jquery writing one click statement for query.map set up instead of 3

I have several divs set up like so:
<div class="answers_total">
<div data-answer="true" class="answer">SPEECH</div>
<div data-answer="false" class="answer">RELIGION AND 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 AND 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>
I'm adding up the true answers with jQuery.map() like so:
var x = $('.answers_total').map(function(i,v){
return $('div[data-answer=true]',v).length; // return how many have
data-answer=true
});
What I'm trying to do is write a super concise function for the click. If
I were to write it out for each one I'd do:
var x = $(".answers_total").map(function(i,v) {
return $('div[data-answer=true]',v).length;
});
clicks = 0;
$(".answer").click(function() {
jthis = this;
if ( $(jthis).att("data-attribute") == "true" ) {
clicks++;
if ( clicks == x[0] ) {
alert('you found all the true answers');
}
}
});
The problem with writing it like this is I'd have to make a version of it
for each "answers_total". How would you change this so you only had to
write it once?
Thanks for any help I can get on this.

No comments:

Post a Comment