Skip to content
Advertisement

switch statement not working in javascript

I am experiencing an issue where a javascript function, is getting a var selectedMonth which is a string, and the alert dialog is showing the value of the string but for some reason the switch statement is not working. I am experiencing this issue on page load, it does work with <select> onchange listener.

in this case, i get alert Dezember but switch statement december doesn’t get called.

function showDropDown(selectedMonth) {

  alert(selectedMonth);

  var settings = {
    "url": "",
    "method": "GET",
    "timeout": 0,
  };

  switch (selectedMonth) {
    case 'Januar':
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/1"
      break;
    case 'Februar':
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/2"
      break;
    case "März":
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/3"
      break;
    case "April":
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/4"
      break;
    case "Mai":
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/5"
      break;
    case "Juni":
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/6"
      break;
    case "Juli":
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/7"
      break;
    case "August":
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/8"
      break;
    case "September":
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/9"
      break;
    case "Oktober":
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/10"
      break;
    case 'November':
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/11"
      break;
    case 'Dezember':
      settings.url = "https://sslbeta.de/api/plantsearchapi/latest/12"
      break;
    default:
      // code block
  }

  alert(settings.url);
}

    $(".New_Plant_Month").change(function () {

        // Fetching Value
        var month = $(this).val();
        showDropDown(month);

    });

// show months
$(document).ready(function() {


  var month = $(".New_Plant_Month  option:selected").text() + "";
  showDropDown(month);

  for (var i = 0; i < 6; i++) {
    var d = new Date();
    var months = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];
    var monthName = months[new Date(d.setMonth(d.getMonth() - i)).getMonth()];

    //console.log(monthName);
    $('.New_Plant_Month').append($("<option>").val(monthName).text(monthName));
}

  


});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="New_Plant_Month">
</select>

Preview enter image description here

asp.net core code

        <!-- asp-items="New plant SelectList"-->
        <div class="form-group">

            <label asp-for="NewPlantMonth" class="control-label">Neue Pflanzen</label>
            <!-- asp-items="NewsSelectList"-->
            <select class="form-control Shoplist New_Plant_Month" onchange="showDropDown(this.value)" data-search="true" asp-for="NewPlantMonth">
                <option value="NewPlantMonth"> @{var selectedValue = Model.NewPlantMonth != null ? Model.NewPlantMonth : "Bitte Monat auswählen"; } @selectedValue </option>
</select>

<span asp-for="NewPlantMonth" class="text-danger"></span>
        </div>

Advertisement

Answer

The only fix i found to this was to trigger<select> via jquery change listener programmatically on page load otherwise the var selectedMonth in switch never passed.. as shown in above gif screenshot.

anyways here is the fix.

        $(document).ready(function () {
              .
              .
              .

            $('.New_Plant_Month')
                .trigger('change');

        });
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement