I use ajax to create a small voting application. When you press +1 to vote for an item, the following code is run, adding it to the vote count, then php returns a reordered list of items by votes. I want the voted item to turn green. Color-changing code must be run after items have been reordered and retrieved.
The following is a block of code. The text between them discusses what this particular part does.
$(function(){ $(".plus").live('click', function() { var plus = $(this).data('plus'); var dataString = "plus="+plus;
The next line of code receives the parent of the pressed button with the .heading class and animates its background color to green to show that it was voted. This works great, except that you only see it for a second, because when you successfully complete an item, they are retrieved and displayed again to reorder them by voice.
$(this).parents(".itemheading").stop().animate({ backgroundColor: "#cdeb8b"}, 100); $.ajax({ type: "POST", url: "bin/processButtons.php", data: dataString, success: function() { $.ajax({url: 'retrieve.php', dataType: 'json'}).done(function(data) { var html = data['html']; $('#content') .html(data['html'])
What I want to do is move the line of code here so that it animates the color AFTER the elements have been voted and then extracted. This does not work anymore, because $(this) needs to be done on the button immediately after it is pressed.
$(this).parents(".itemheading").stop().animate({ backgroundColor: "#cdeb8b"}, 100); }); } }); return false; }); });
Thanks.
source share