views:

35

answers:

1

i have got this script that works fine, that allows a user to vote, but im kind of stuff of how i can make the vote button, when clicked again it toggle back to normal image! just like stackoverflow:

this is my 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) {

                $(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:

<ul class="statuses">
<li id="set_41" class="message">
 <span class="vote_count">0</span>
 <a href="#" class="vote_up"><img src="img/uparrow.png" /></a>

this is the psuedo lanaguge that i want to happen:

a user clicks vote image, it changes the image(uparrowActive)
//ive done that already
then
if user clicks the same image again it goes back to normal image(uparrow)
A: 

jquery has a toggle event that allows you to bind two event handlers to an element that are called alternately.

*untested, probably has simple syntax errors.

myElement.toggle(vote(), unvote());

function vote()
{
   // add a vote to the item
   // change icon to vote icon
}

function unvote()
{
  // remove a vote from the item
  // change icon to default icon.
}
Matthew Vines
so if instead of me using click, i use toggle? ;)
getaway
and remeber i need to change the vote on the ajax request so i dont what to do!!
getaway
i still dont understand, im a new bie to javascript and jquery
getaway