jQuery $(this) does not work at all in my codes, here are the codes. The codes sending from my HTML is an image's onlick 'vote', located in a tb element of table.
function vote(type, value, musicId) {
var dataFields = {
'type': type,
'value': value,
'musicId': musicId
};
$.ajax({
type: "POST",
url: "ajax/voting.php",
data: dataFields,
timeout: 3000,
success: function(dataBack) {
$(this).css("background-color", "rgba(255,255,0,0.7)");
},
error: function() {
$('#message').text('Problem!');
}
});
}
HTML & PHP codes:
echo '<td class="up"><img class="liked" onclick="vote(\'liked\', \'1\', '.$single_music['id'].'); return false;" src="img/layout/like.png"></td>';
echo '<td class="down"><img class="favorite" onclick="vote(\'favorite\', \'1\', '.$single_music['id'].'); return false;" src="img/layout/favorite.png"></td>';
What i want to do?
Because there are multiple items generated from database, and i want to let the jquery tell them apart when i click the vote image in some item.
Could you please help me find where the error is? Thanks.
onclick
attribute. When thevote()
function is executed the value ofthis
will already bewindow
. As you're already using jQuery you should use.on()
instead - like in this fiddle with a modified version of your markup+script