title: Function call in jquery each loop on ajax success returns undefined variable
body: <p>In the code below  the <code>position</code> variable in the <code>$.each</code> loop of the ajax success method returns undefined when <code>btn_search</code> is clicked. 
  What I want to achieve is to assign the value of the <code>checkPosition</code> function to the variable <code>position</code> in the <code>$.each</code> loop so I can use the value of the variable to determine what results to display. How can I go about it?</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
     var url = "{{URL::route('find.paid.trade.cat') }}";
     var position = '';
  
  $(function(){
  
     $('#btn_search').click(function(e){
  
      $("#results_tbl").empty();
      var category_id = $('#trade_category').val();
     $.ajax({
        method:'POST',
        url: url,
        data : {
            category_id : category_id,
           },
        success:function(response){
  
          $.each(response.result, function(index, value){ 
  
             position = checkPosition(response.result[index]['id']);
            console.log(position);
             if(position == 1){
              $('#results_tbl').append('&lt;tr&gt;&lt;td&gt;'+response.result[index]['business_name']+
                      '&lt;/td&gt;&lt;td&gt;&lt;button data-id="'+response.result[index]['id']+'" data-cat_id="'+response.result[index]['business_trade_id']+'"'+
                      'data-subscription_id="'+response.result[index]['active_subscription_id']+'" class="btn_make_1st btn-primary btn"&gt;1st Positions&lt;/button&gt;'+
                      '&lt;button data-id="'+response.result[index]['id']+'" class="btn btn_make_2nd btn-info"&gt;Make 2nd&lt;/button&gt;'+
                      '&lt;button data-id="'+response.result[index]['id']+'" class="btn btn_make_3rd btn-success"&gt;Make 3rd&lt;/button&gt;&lt;/td&gt;'+
                      '&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;')
             }
            else
             {
                  $('#results_tbl').append('&lt;tr&gt;&lt;td&gt;'+response.result[index]['business_name']+
                      '&lt;/td&gt;&lt;td&gt;&lt;button data-id="'+response.result[index]['id']+'" data-cat_id="'+response.result[index]['business_trade_id']+'"'+
                      'data-subscription_id="'+response.result[index]['active_subscription_id']+'" class="btn_make_1st btn-primary btn"&gt;Make 1st&lt;/button&gt;'+
                      '&lt;button data-id="'+response.result[index]['id']+'" class="btn btn_make_2nd btn-info"&gt;Make 2nd&lt;/button&gt;'+
                      '&lt;button data-id="'+response.result[index]['id']+'" class="btn btn_make_3rd btn-success"&gt;Make 3rd&lt;/button&gt;&lt;/td&gt;'+
                      '&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;')
             }
  
          })
        },
        error:function(){
  
        }
     })
  
  });
  
  
  
  });
  
  
  
  });
  
  
   })
  
  
  
  function checkPosition(trade_id)
  {
      var trade_current_position_url = "{{URL::route('find.position') }}";
      var current_position ;
      $.ajax({
        method:'POST',
        url:trade_current_position_url,
        data:{
          trade_id: trade_id
        },
        success:function(response){
           current_position = response.current_pos;
           returnPosition(current_position);
        },
        error:function(){
  
        }
      });
  
  }
  
  
  
  function returnPosition(position)
  {
  return position;
  }
  </code></pre>
  
  <p></p>