title: Pass option value from one function to another function
body: <p>I have a function like this:</p>
  
  <pre><code>  $('input[type=radio][name=editList]').change(function() {
                      if (this.value == 'Proveedor') {
                          $.getJSON("@Url.Action("GetProveedores", "Agenda")",
                              function (data) {
                                  var items = "";
                                  $.each(data,
                                      function(index, item) {
                                          items += "&lt;option value='" + item.ID + "'&gt;" +item.ID+item.NombreComercial + "&lt;/option&gt;";
                                      });
                                  $("#lstProveedor").html(items);
                              });
  </code></pre>
  
  <p>I want to get that item.ID to use into another function as:</p>
  
  <pre><code> $(function getCuadrilla() {
      var items = "";
     $.getJSON("@Url.Action("GetCuadrillas", "Agenda")" + "?ProveedorID=" + ID, function (data) {
  
     $.each(data, function (index, item) {...
  
                              });
                              $("#lstcuadrilla").html(items);
                          });
  
                      });
  </code></pre>
  
  <p>I try to use <code>$('input[type=radio][name=editList]').val()</code> as </p>
  
  <pre><code>    $(function getCuadrilla() {
              var items = "";
  $('input[type=radio][name=editList]').val();
   $.getJSON("@Url.Action("GetCuadrillas", "Agenda")" + "?ProveedorID=" + ID, function (data) {
  
             $.each(data, function (index, item) {...
  
                                      });
                                      $("#lstcuadrilla").html(items);
                                  });
  
                              });
  </code></pre>
  
  <p>but instead <code>item.ID</code> I receive string <code>"Proveedor"</code>, can any one explain me how can I access to item.ID? Regards</p>
  
  <p><strong>Complete JS:</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  
    $(function getResponsable() {
      $('input[type=radio][name=editList]')
        .change(function() {
          if (this.value == 'Proveedor') {
            $.getJSON("@Url.Action("GetProveedores ", "Agenda ")",
              function(data) {
                var items = "";
                $.each(data,
                  function(index, item) {
                    items += "&lt;option value='" + item.ID + "'&gt;" + item.ID + item.NombreComercial + "&lt;/option&gt;";
                  });
                $("#lstProveedor").html(items);
              });
          } else if (this.value == 'Sucursal') {
            $.getJSON("@Url.Action("
              GetUnidades ", "
              Agenda ")",
              function(data) {
                var items = "";
                $.each(data,
                  function(index, item) {
                    items += "&lt;option value='" + item.ID + "'&gt;" + item.Codigo + "-" + item.Nombre + "&lt;/option&gt;";
                  });
                $("#lstProveedor").html(items);
              });
  
          } else if (this.value == 'Usuario') {
            $.getJSON("@Url.Action("GetUsuario ", "Agenda ")",
              function(data) {
                var items = "";
                $.each(data,
                  function(index, item) {
                    items += "&lt;option value='" + item.ID + "'&gt;" + item.Nombre + " " + item.Apellido + "&lt;/option&gt;";
                  });
                $("#lstProveedor").html(items);
              });
          }
        });
  
      $(function getCuadrilla() {
        var items = "";
       var ID = $("#lstProveedor").val();
        $.getJSON("@Url.Action(" GetCuadrillas ", "Agenda ")" + "ProveedorID=" + ID,
          function(data) {
  
            $.each(data, function(index, item) {
              items += "&lt;option value='" + item.ID + "'&gt;" + item.NombreComercial + "&lt;/option&gt;";
            });
            $("#lstProveedor").html(items);
          });
  
      });
  
    });
  </code></pre>
  
  <p>As James comment, I try it and JS hit first line but it don´t pass over there 
  <a href="https://i.stack.imgur.com/Jcrn1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jcrn1.png" alt="enter image description here"></a></p>
  
  <p>Update:</p>
  
  <p><strong>New js structure:</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
          $(function getResponsable() {
              $('input[type=radio][name=editList]')
                  .change(function() {
                      if (this.value == 'Proveedor') {
                          $.getJSON("@Url.Action("GetProveedores", "Agenda")",
                              function(data) {
                                  var items = "";
                                  $.each(data,
                                      function(index, item) {
                                          items += "&lt;option value='" +
                                              item.ID +
                                              "'&gt;" +
                                              item.ID +
                                              item.NombreComercial +
                                              "&lt;/option&gt;";
                                      });
                                  $("#lstProveedor").html(items);
                              });
                      } else if (this.value == 'Sucursal') {
                          $.getJSON("@Url.Action("GetUnidades", "Agenda")",
                              function(data) {
                                  var items = "";
                                  $.each(data,
                                      function(index, item) {
                                          items += "&lt;option value='" +
                                              item.ID +
                                              "'&gt;" +
                                              item.Codigo +
                                              "-" +
                                              item.Nombre +
                                              "&lt;/option&gt;";
                                      });
                                  $("#lstProveedor").html(items);
                              });
  
                      } else if (this.value == 'Usuario') {
                          $.getJSON("@Url.Action("GetUsuario", "Agenda")",
                              function(data) {
                                  var items = "";
                                  $.each(data,
                                      function(index, item) {
                                          items += "&lt;option value='" +
                                              item.ID +
                                              "'&gt;" +
                                              item.Nombre +
                                              " " +
                                              item.Apellido +
                                              "&lt;/option&gt;";
                                      });
                                  $("#lstProveedor").html(items);
                              });
                      }
  
                  });
              $("#lstProveedor").change(function() {
                  var items = "";
                  var ID = $("#lstProveedor").val();
                  $.getJSON("@Url.Action("GetCuadrillas", "Agenda")" + "ProveedorID=" + ID,
                    function(data) {
                        $.each(data, function(index, item) {
                            items += "&lt;option value='" + item.ID + "'&gt;" + item.NombreComercial + "&lt;/option&gt;";
                        });
                        $("#lstProveedor").html(items);
                    }
      );
              });
          });
      &lt;/script&gt;
  </code></pre>