mardi 8 décembre 2015

Getting value of last clicked item from array

Where I'm At

I've console.log(buttons[i].value) and console.log(buttons[i].class) and can see that the for loop is printing the class and the value of these six buttons, which represent a bid in a silent auction $10, 25, 50 and have been stored in an array called var buttons = [], like I want.

Problem

However, what I need to do is get the value of the last clicked button from the array and that value to the value of .current__amount. Then replace the placeholder tk-amount text with the total of those two amounts to make the .new__amount on click.

scripts.js

/*-------------------------------------
STEP ONE: PLACE BID
--------------------------------------*/

// Bid Options
var buttons = [
    { "class": "button__one", value: 10 },
    { "class": "button__two", value: 25 },
    { "class": "button__three", value: 50 },
    { "class": "button__four", value: 100 },
    { "class": "button__five", value: 250 },
    { "class": "button__six", value: 500 }
];

$(".button__form").on('click', function(){
    var btnSelected = $(this).hasClass("is-selected");
    var sectionOneCompleted = $(".check--one").hasClass("is-completed");

    if (btnSelected) {
        $(this).removeClass("is-selected");
        $(".check--one").css("color", "#ccc");
    } else {
        $(".button__form").removeClass("is-selected");
        $(this).addClass("is-selected");
        $(".check--one").css("color", "#ffdc00");
    }
    console.log("Clicked");
});

/*-------------------------------------
API: SHEETSU
--------------------------------------*/

$.ajax({
    url: "http://ift.tt/1TzrQ02",
    method: "GET",
    dataType: "json"
}).then(function(spreadsheet) {

    // Print current bid
    var currentBid = parseInt(spreadsheet.result.pop().Bids);
    $(".current__amount").html("$" +currentBid);

    // Any of the form buttons
    var $btnForm = $(".button__form");

    for (i = 0; i < buttons.length; i++) {
        if ($btnForm.hasClass(buttons[i].class)){
            $(".new__amount").html("$" + (currentBid + buttons[i].value));
            console.log(buttons[i].value);
            console.log(buttons[i].class);
        }
    }
});

Aucun commentaire:

Enregistrer un commentaire