Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get in 'clicked' element in jquery ajax success callback

This is one easy (I hope) question which is bugging me for years. How do you get element that fired event which triggered the ajax request? Here is an example:

<button id="clickme">Click me</button>
$("#clickme").click(function(e){
    var i_need_this=e.target;
    alert(i_need_this); //nice!
    $.ajax({
            url:'http://echo.jsontest.com/',

            type: 'GET',
            success: function( data, status, jqXHR  ) {
                alert('success');
              //console.log( ??? ); Get i_need_this from somewhere?       
            }
        });

})

Live: http://jsfiddle.net/8F3u2/

Thanks in advance!

like image 624
Amantel Avatar asked Dec 26 '22 06:12

Amantel


1 Answers

You can use context param here:

$.ajax({
        url:'http://echo.jsontest.com/',
        context:this, // <------this is the cliked button
        type: 'GET',
like image 132
Jai Avatar answered Apr 27 '23 01:04

Jai