I'm trying to add a function Fvote
to all elements with class vote-up
and vote-down
.
var voteup = document.getElementsByClassName("vote-up");
var votedown = document.getElementsByClassName("vote-down");
function Fvote(upordown,postid) {
var x=this;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {console.log(xmlhttp.responseText);
if(xmlhttp.responseText=="error")
;
else{
/*some JS actions*/;}
}
xmlhttp.open("GET", "ajax/vote.php?q=" + postid + "q2="+upordown, true);
xmlhttp.send();
}
for(var i=0;i<voteup.length;i++)
voteup[i].addEventListener('click', Fvote("up",voteup[i].getAttribute("data-vote")), false);
for(var i=0;i<votedown.length;i++)
votedown[i].addEventListener('click', Fvote("down",votedown[i].getAttribute("data-vote")), false);
But when I load the page, it runs the function Fvote
many times as the count of elements number, without clicking on any item. and if I clicked on items with class of vote-up
or vote-down
the function is not called. What I'm doing wrong?