Result of jQuery keyup event handler in switch statement not returned

307 views Asked by At

I have a switch Statement in JavaScript in which the keyup event handler doesn't return the callback to my variable (var test in this case).

It is the last case (110) that doesnt work.

Here is the code. Hope you can help to enlight me:

var getDateRange;

$(".nav-reports").append(
  '<div id="daterange-container">' +
  '<select id="daterange">' +
  '<option value="0">Choose duration:</option>' +
  '<option value="10">Today</option>' +
  '<option value="20">Yesterday</option>' +
  '<option value="30">This week</option>' +
  '<option value="40">Last week</option>' +
  '<option value="50">This month</option>' +
  '<option value="60">Last month</option>' +
  '<option value="70">This year</option>' +
  '<option value="80">Last year</option>' +
  '<option value="100">Everything</option>' +
  '<option value="110">Last x days</option>' +
  '</select>' +
  '</div>'
);

getDateRange = function () {
  var test;
  var dateString = "yyyy-MM-dd";
  var selectedOption = parseInt($('#daterange').change().val());
  $('.daterange-input').detach();
  switch (selectedOption) {
    case 10: // works
            break;
    case 20: // works
            break;
    case 30: // works
            break;
    case 40: // works
            break;
    case 50: // works
            break;
    case 60: // works
            break;
    case 70: // works
            break;
    case 80: // works
            break;
    case 100: // works
            break;
    case 110: // doesnt't work
            $('#daterange-container').append('<input class="daterange-input" type="text" maxlength="4">');
            $('.daterange-input').keyup(function () {
              test = '/' + Date.today().addDays(-parseInt($(this).val())).toString(dateString) + '/' + Date.today().toString(dateString);
            });
            break;
  }
  return test;
};

Thank you in advance.

I call the getDateRange function here:

$('.' + report.description).click(function () {
                    var dateRange = getDateRange();
// some more code not in relation
                })
1

There are 1 answers

2
programisto On

Yeah. Thanks to @Bellian I got it.

Defined test outside of getDateRange and removed the return test statement.

Now I call getDateRange before assigning var dateRange.

Here is the working code:

 var test;

    getDateRange = function () {
        var dateString = "yyyy-MM-dd";
        var selectedOption = parseInt($('#daterange').change().val());
        $('.daterange-input').detach();
        switch (selectedOption) {
            case 110:
                $('#daterange-container').append('<input class="daterange-input" type="text" maxlength="4">');
                $('.daterange-input').keyup(function () {
                    test = '/' + Date.today().addDays(-parseInt($(this).val())).toString(dateString) + '/' + Date.today().toString(dateString);
                });
                break;
        }
    };

And then:

$('.' + report.description).click(function () {
    getDateRange();
    var dateRange = test;
});

Thanks @Bellian and to all the others.