lundi 25 juillet 2016

return an object that show which element was clicked

I want to click a div called submitButton this should tell me what the users have done. I have 3 sections. A voting section, a review section, and another type of voting section. the point is that right now I'm trying to set up the first voting section. When I click the submitButton I should get a an object that looks like {vote:down} or {vote:up} I only have two buttons in the voting section.

function gatherData(){
    var submitButton = ".submitButton";
    var result = {}
    function voteSection(){
        $(".upButton").click(function(){
            // result.vote = "up"
            //  console.log(result) ;
            $(this).data("clicked", true);
            $(this).siblings(".downButton").data("clicked",false)
        })
        $(".downButton").click(function(){
            $(this).data("clicked", true);
            $(this).siblings(".upButton").data("clicked",false)
            // result.vote = "down";
            //  console.log(result) ;
        })
    //    return result;
    }
    $(submitButton).on("click",function(){
        if($(".upButton").data("clicked")){
            result.vote = "up"
        }else if($(".downButton").data("clicked")){
            result.vote = "down";
        }
    })
    return result;
}
$(".submitButton").on("click",function(){
    console.log(gatherData())
})

Thanks for any help

Aucun commentaire:

Enregistrer un commentaire