JQuery Select element called AJAX

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

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

It makes sense? Another way to explain this. I want a bunch of buttons that, when clicked, make an ajax call, and when that call completes, the button that was clicked is removed.
+3


source to share


1 answer


Yes it makes sense, use the ajax property context

:

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

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

      

context:



This object will become the context for all Ajax-related callbacks. From default, a context is an object that represents the ajax settings used in the call ($ .ajaxSettings, combined with the parameters passed to $ .ajax). For example, specifying a DOM element, since the context will be that context for the full request callback.

ajax docs

+1


source







All Articles