views:

64

answers:

2

im trying to turn this jquery script into a toggle event type, thier is my script, with illustrations, to show what i want to happen. i cnt seem to intrgate it so here goes:

//myElement.toggle(vote(), unvote()); // this is the toggle event

    $(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();

//function vote () // this should be the vote up function
                //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");
                    }
                });

//function unvote() thier should be another function here to unvote toggle

$.ajax({
                    context: this,
                    type: "POST",
                      // Make sure "this" in the callback refers to the element clicked

                    data: "action=vote_down&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/uparrow.png");
                    }
                });
            });

i just dont know how to integrate this toggle event into this jquery snippet, someone please help me ive been trying for years :)) thanks

+1  A: 

Doing so:

$('.statuses').delegate('.vote_up, .vote_down', 'click', function(e) {
    if($(this).hasClass('.vote_up') {
        src = "img/uparrowActive.png";
        action = "vote_up";
    }
    else {
        src = "img/uparrow.png";
        action = "vote_down";
    }

            $.ajax({
                context: this,
                type: "POST",
                  // Make sure "this" in the callback refers to the element clicked

                data: "action=" + action + "&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", src);
                }
            });
});
Arnaud F.
thanks brillant answer, but i need it to toggle, hows jquery going to know when the vote is up or down, so it can toggle, just like stackoverflow! :)) thanks again
getaway
Easily by testing the <img> src attribute, if src is "img/uparrow.png", then change src to img/uparrowActive.png and action too. ;)
Arnaud F.
oh okay, so it already deos this
getaway
If you want to improve this, I recommend you to use .data(). In your case, when you voted up, add $(this).closest('.statuses')data('voted_up'); then test it if you want to vote down
Arnaud F.
i still dnt understand, excuse my stupidity! :)) lol
getaway
Will add a new post, will be more comprehensive.
Arnaud F.
A: 
$('.statuses').delegate('.vote_up, .vote_down', 'click', function(e) {
    var parent = $(this).closest('.statuses'),
        vote = (($(this).hasClass("vote_up")) ? "up" : "down");

    if(parent.data("vote") == vote) {
        return true; // If already voted up, do nothing, if already voted down, do nothing...
    }

    if(vote == "down") { // Vote down
        src = "img/uparrow.png";
        action = "vote_down";
    }
    else if(vote == "up") { // Vote up
        src = "img/uparrowActive.png";
        action = "vote_up";
    }

    // Setting current vote (up or down)
    // So next time you toggle, we have the trace of the previous vote (up/down)
    // And authorise only do the opposite
    parent.data("vote", vote);

            $.ajax({
                context: this,
                type: "POST",
                  // Make sure "this" in the callback refers to the element clicked

                data: "action=" + action + "&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", src);
                }
            });
});
Arnaud F.
thank you very much, but now its not even working at all like before.. but thanks, i will give you the right answer, im just gonna ask another question anyway!
getaway
Sorry, it looks like I don't understand your need (my fault ;))
Arnaud F.