1

I have a function that will display the data in the closest tr inside the api function. The problem is that they can't find the closest tr. Here's my code:

$('.pricing_level').on('change', function(e){
    var pricing_level_id = e.target.value;
    $('tbody.subcontent tr').each(function(){
        var item_id =  $(this).closest('tr').find(".details_itemid").val();

        var category_id = $(this).closest('tr').find(".details_category_id").val();
        var subcategory_id = $(this).closest('tr').find(".details_subcategory_id").val();
        var net_unit_price = $(this).closest('tr').find(".details_unitprice").val();

        $.get('/dev/api/itempricinglevel?item_id=' + item_id + '&pricinglevel_id=' + pricing_level_id, function(itempricinglevel){

          $(this).closest('tr').find(".details_unitprice").val(itempricinglevel.min);
        })
    }) 
})
Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184

1 Answers1

1

Assign this variable to another before the ajax call is made, because inside the ajax the this variable gets over written by the jqXHR object of the Ajax call.

$('.pricing_level').on('change', function(e){
    var pricing_level_id = e.target.value;
    $('tbody.subcontent tr').each(function(){
        var item_id =  $(this).closest('tr').find(".details_itemid").val();

        var category_id = $(this).closest('tr').find(".details_category_id").val();
        var subcategory_id = $(this).closest('tr').find(".details_subcategory_id").val();
        var net_unit_price = $(this).closest('tr').find(".details_unitprice").val();
        var that = this;

        $.get('/dev/api/itempricinglevel?item_id=' + item_id + '&pricinglevel_id=' + pricing_level_id, function(itempricinglevel){
            $(that).closest('tr').find(".details_unitprice").val(itempricinglevel.min);
        })
    }) 
})
rrk
  • 15,677
  • 4
  • 29
  • 45