views:

56

answers:

1

i have this voting jquery script, which works fine, but i want the option for the user to toggle the vote, just like stackoverflow, so basically

you can vote up by clicking up arrow
   increase vote by 1
   if they click again on the picture(arrow up)
   minus 1 from the vote

my jquery script:

$(document).ready(function() {
    $('.statuses').delegate('.vote_up', 'click', function(e) {

        //stop event
        e.preventDefault();
       //get the id
        var the_id = $(this).closest('.message').attr('id').split('_').pop();
        //the main ajax request
        $.ajax({
            context: this,
            type: "POST",
              // Make sure "this" in the callback refers to the element clicked

            data: "action=vote_up&id=" + the_id,
            url: "ajax/votes.php",
            success: function (msg) {
                  // Not sure where your vote_count is. See the HTML for my placement
                $(this).siblings("span.vote_count").html(msg).fadeIn();
                  // get the child <img> and set its src
                $(this).children("img").attr("src", "img/uparrowActive.png");
            }
        });
    });
});

html code:

<span class="vote_count">0</span>
            <a href="#" class="vote_up"><img src="img/uparrow.png" /></a>
            <a href="#" class="vote_down"><img src="img/downarrow.png" /></a>

i was thinking what was the best way to do this thanks :))