user3816016
user3816016

Reputation: 3

add different variable for each div in same time

I want to create a countdown for each DIV (.topitembox) and for that I have to add those json vars

$('#countdown_items .topitembox').each(function () {
    itmID = $(this).attr('class').replace(/[^0-9]/g, '');
    $(this).append('<div id="countdown' + itmID + '"></div>');
    $.get(getTimeLeft, function (data) {
        var Day = data.Day,
            Hours = data.Hour,
            Mins = data.Min,
            Secs = data.Sec,
            dayH = Day * 24,
            Hours = Hours + dayH;

        // i have more id's: countdown1, countdown2... I need to get the variable dates for each div id
        $('#countdown1').countdowntimer({
            hours: Hours,
            minutes: Mins,
            seconds: Secs,
            timeUp: timeisUp
        });

    }, "json");
});

I tried with $(this) inside json and it's not working. There's another function for this?

Upvotes: 0

Views: 477

Answers (1)

Satpal
Satpal

Reputation: 133423

You need to wrap $.get in a closure.

$('#countdown_items .topitembox').each(function () {
    var itmID = $(this).attr('class').replace(/[^0-9]/g, '');
    $(this).append('<div id="countdown' + itmID + '"></div>');

    (function (itmID) { //Notice Here
        $.get(getTimeLeft, function (data) {
            var Day = data.Day,
                Hours = data.Hour,
                Mins = data.Min,
                Secs = data.Sec,
                dayH = Day * 24,
                Hours = Hours + dayH;

            //Now itmID is accesible here
            $('#countdown' + itmID).countdowntimer({ //Notice Here
                hours: Hours,
                minutes: Mins,
                seconds: Secs,
                timeUp: timeisUp
            });

        }, "json");

    })(itmID);//Notice Here
});

Upvotes: 1

Related Questions