title: Using a local variable out of an event handler function that makes an ajax call in Javascript
body: <p>What would be the best way to get the value of the variable "search" from the first function to use it on the last ajax call? I know I could use closure but not sure how. Here's my code:</p>
  
  <pre><code>$(document).on("click", ".box", function(){
      var search = $(this).attr("alt");
      $.ajax({
          method: "GET",
          url: url + search,
          success: function(data){
            console.log(data);
          }
      })
  });
  
  $( ".select" ).change(function(){
   var param2 = $(this).val();
   $.ajax({
      method: "GET",
      url: url + search + param2,
      success: function(data){
        console.log(data);
      }
    })
  });
  </code></pre>