I’ve trying to put two icons for vote up/down but I’m not sure how exactly to do this. Current code havehasonly two <input type="button">
now need to put icons on them. This is what I try so far:
<div class="btn-votes">
<div class="like">
<i class="icon-like">
<input type="button" title="Up" class="up" onClick="addVote('.$row['image_id'].','1')" '.$up.' />
</i>
</div>
<div class="dislike">
<i class="icon-dislike">
<input type="button" title="Down" class="down" onClick="addVote('.$row['image_id'].','-1')" '.$down.' />
</i>
</div>
</div>
And this is the AJAX part.
function addVote(image_id,vote_rank) {
$.ajax({
url: "add_vote.php",
data:'image_id='+image_id+'&vote_rank='+vote_rank,
type: "POST",
beforeSend: function(){
$('#links-'+image_id+' .btn-votes').html("<img src='img/loaderIcon.gif' />");
},
success: function(vote_rank_status){
var votes = parseInt($('#votes-'+image_id).val());
var vote_rank_status;
switch(vote_rank) {
case "1":
votes = votes+1;
break;
case "-1":
votes = votes-1;
break;
}
console.log(vote_rank_status);
$('#votes-'+image_id).val(votes);
$('#vote_rank_status-'+image_id).val(vote_rank_status);
var up,down;
if(vote_rank_status == 1) {
up="disabled";
down="enabled";
}
if(vote_rank_status == -1) {
up="enabled";
down="disabled";
}
var vote_button_html = '<input type="button" title="Up" id="up" onClick="addVote('+image_id+','1')" '+up+' /><div class="label-votes">'+votes+'</div><input type="button" title="Down" id="down" onClick="addVote('+image_id+','-1')" '+down+' />';
$('#links-'+image_id+' .btn-votes').html(vote_button_html);
}
});
}
I’m not sure where is the problem but in web console I see the error
ReferenceError: addVote is not defined
which I think mean that can’t pass onClick
event to the AJAX function. So how should those buttons be?
This is the current HTML which is working just fine
<div class="btn-votes">
<input type="button" title="Up" class="up" onClick="addVote('.$row['image_id'].','1')" '.$up.' />
<input type="button" title="Down" class="down" onClick="addVote('.$row['image_id'].','-1')" '.$down.' />
</div>
UPDATE: Current code
<div class="btn-votes">
<div class="like">
<button title="Up" class="up" onClick="addVote('.$row['image_id'].','1')" '.$up.' />
<i class="icon-like"> </i>
</button>
</div>
<div class="dislike">
<button title="Down" class="down" onClick="addVote('.$row['image_id'].','-1')" '.$down.' />
<i class="icon-dislike"></i>
</button>
</div>
</div>
2
Answers
First, the addVote() function does not exist, so wrap your ajax call in it.
Then, do not use
<input type="button" />
, but<button></button>
, and put the icon inside.If you are already using jquery, you might wanna drop the onclick and do something like:
And echo the php in
data
attributes of course.You can bind onclick event on button, however, you should bind the function by jquery and not by onclick, as the onclick will trigger outside of your function scope :
Note that if you have multiple vote button on your page, you MUST change the id up and down from id to class as id MUST BE UNIQUE