DanRedux
DanRedux

Reputation: 9349

jQuery Select the element that called the AJAX

I have two buttons, and their click events perform an ajax operation.

I need to know how to change the element that called it, for example, if you click on the first button, the ajax uses the first button in its "success" function to change its value to the data returned.

Does this make sense? Another way to explain it.. I want a bunch of buttons that, when clicked, perform an ajax call, and when that call completes the button that was clicked gets deleted.

Upvotes: 3

Views: 220

Answers (1)

gdoron
gdoron

Reputation: 150253

Yes, it make sense, use the ajax context property:

function doAjax() {
    $.ajax({
        url: "test.html",
        context: this, // <===
        success: function() {
            $(this).hide();
        }
    });
}​    

$('input[type="button"]').click(doAjax);

context:

This object will be made the context of all Ajax-related callbacks. By default, the context is an object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax). For example specifying a DOM element as the context will make that the context for the complete callback of a request.

ajax docs

Upvotes: 1

Related Questions