1

My problem is that i want to get a value of a function but its returning [object][object]. just see my code.where is the error..

function existing_time(nxtDay) {
    var time = 1;
    return $('input[name="date[]"]').each(function () {
        var selectedDate = $(this).val();
        var idName = $(this).get(0).id;
        var times = 1;
        if (selectedDate == nxtDay) {
            //alert(idName);
            //alert(selectedDate);
            var idNo = idName.substr(10);
            alert(idNo);
            times = $("#timepicker_" + idNo).val();
            alert(times);
            console.log("times", times)
            return times;
        }
    });
    //alert(time_1);
    if (time_1 == 1) {
        return time;
    } else {
        return time_1;
    }
}

In loop it alerts correct value bt i could not get the value outside the function.please help me...

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 2
    `.each` ignores the return value from the callback (almost, https://api.jquery.com/each/) and simple returns a jQuery object. If you want to get a value inside the callback to the outside, *assign* it to a variable in the outer scope. – Felix Kling Jul 11 '14 at 06:39
  • What should it return, though? The first time found or all the times that matched? – Ja͢ck Jul 11 '14 at 06:44

1 Answers1

0

I think that what you want is something like this:

var time=1;
var sum = 0;
$('input[name="date[]"]').each(function(){

var selectedDate=$(this).val();

var idName = $(this).get(0).id;

var times=1;

if(selectedDate==nxtDay)

{

    //alert(idName);

    //alert(selectedDate);

    var idNo = idName.substr(10);

    alert(idNo);

    times = $("#timepicker_"+idNo).val();

    alert(times);

    console.log("times", times)

    sum += times;

}

});

return sum;

As far as I know jQuery.each is not going to return a number, probably its a jQuery object.

  • no i want to return a string like '08:30' and is there any solution? is there any other option to loop it? actually the field is dynamic and its id also different like datepicker1, datepicker2.... or datepicker1_1, datepicker1_2, datepicker3_1... – sukanta das bairagya Jul 11 '14 at 06:45