'Combo Box clicked value to jQuery

In my ASP.NET MVC application, There are two combo boxes.

1st combo box contains the companies and with the selection, the second combo box loads the values.

Here when the user selects a value from the second combo box I need to get that value Id to the jQuery.

So far in my code, the selected value comes after the next selection. As an example, If I clicked a value, and again click another value then I get the ID of my previous selected value.

Can I get help to fix that, I need to get the selected value from the second combo box.

This is the HTML code

<div class="row">
  <div class="col-md-6 col-sm-6">
    <div class="form-group"> @Html.LabelFor(model => model.CompanyID, htmlAttributes: new { @class = "control-label col-md-6" }) <div class="col-sm-8"> @Html.DropDownListFor(model => model.CompanyID, Model.CompanyLists, "Select Company List", new { @id = "ddlComTypes" + Model.TempID, @class = "js-dropdown js-Com", @data_map = Model.TempID }) @Html.ValidationMessageFor(model => model.CompanyID, "", new { @class = "text-danger" }) </div>
    </div>
  </div>
  <div class="col-md-6 col-sm-6">
    <div class="form-group"> @Html.LabelFor(model => model.EmpId, htmlAttributes: new { @class = "control-label col-md-6" }) <div class="col-md-8"> @Html.DropDownListFor(model => model.EmpId, new List <SelectListItem>(), new { @id = "ddlEmpId" + Model.TempID, @class = "js-dropdown js-emp" , onchange = "OnSelectedIndexChange();" }) @Html.ValidationMessageFor(model => model.EmpId, "", new { @class = "text-danger" }) </div>
    </div>
  </div>
</div>

This is what I tried

< script type = "text/javascript" >

  function OnSelectedIndexChange() {
    var b = $('.js-Com').val();
    var a = $('.js-emp').val();
    alert(b);
    alert(a);

  }

  <
  /script>



Solution 1:[1]

You can just write an event handler for the second combo box:

$("#secondComboBox).on("change", function () {
    const selectedId = $(this).val();
    // You might want to int.parse($(this).val()) if you need an integer type.
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 psiodrake