Making a basic poll with javascript/jquery -
just starting learn javascript/jquery.
for recent task i've been asked create function listens clicks on 2 buttons , looks @ "data-vote
" attribute see voted for.
i need set event listener on buttons 'vote
' class, both of have 'data-vote
' attribute, 1 'great
', other 'greatest
'. when 1 of buttons
clicked need @ attribute determine was, , increment counter affected tally.
i know seems simple, i'm lost. here's code far :
$('.vote').on('click', function(event){ var targetelement = event.target; $(targetelement).find('data-vote').each(function(){ if .attr(great)
any @ appreciated.
var great = 0; var greatest = 0; $('.vote').on('click', function() { var value = $(this).data('vote'); if(value === 'great') { great++; } else if(value === 'greatest') { greatest++; } });
the key here inside click
handler, $(this)
refers element clicked.
you can query attribute data-vote
, value. notice we're using jquery's data
method, queries html5 data-*
attributes.
to increment counters, can use 2 global variables great
, greatest
. depending on value of data-vote
of button clicked, can increment corresponding global variable using ++
operator (this called increment arithmetic operator). atomic operator mutates value in place.
Comments
Post a Comment