0

I have a page with 1-N tables of courses, and I'm trying to get my ajax function to update the table that called it, and not every table, or just one.

$(document).on('click',".addCourse",function(e){
    var courseTable = $('#courseList1');
    HideDialog();
    $.ajax({
        type:'POST',
        data:'{}', 
        url:'/degreebuilder/degree/createCourse',
        success:function(data,textStatus,courseTable){
            $(courseTable).html(data);
            },
        error:function(XMLHttpRequest,textStatus,errorThrown){}
    });

});

How do I pass the success function the var courseTable?

This isn't a duplicate to what was linked. This is asking how to use a variable for the success function when updating a DOM element. I have 1, possibly N tables of data, each table can call this POST function, and I need it to update the table that called it.

This works just fine. Spelling helps a ton when you are referencing DOM elements.

idonaldson
  • 465
  • 1
  • 14
  • 33

1 Answers1

3

If the courseTable variable is in scope when you call $.ajax, it will also be in scope when the success callback is hit. Alternatively, you could do:

var courseTable = ();
$.ajax({
    //...
    success: $.proxy(function (table) {
        //table is a reference to courseTable
    }, null, courseTable)
});
defrost
  • 396
  • 2
  • 6