How to use $(this) on AJAX success?

Shota

There are buttons in the each rows of the table. When user click it, a sweetalert is fired to display confirmation window. If user click yes, then an AJAX is executed to post the row values.

The post process is working, but I am unable to change the button attribute and a label text after AJAX success call. This is my script.

$("#tbl_list_pemenuhan tbody").on('click','.btn_memo', function() {                 

    var no_penuh    = $(this).closest('tr').find("td:eq(1)").text();
    var no_minta    = $(this).closest('tr').find("td:eq(2)").text();

    var data        = 'no_pnh='+no_penuh+'&no_mnt='+no_minta;


    swal({
        title               : "Apa anda yakin?",
        type                : "warning",
        showCancelButton    : true,
        confirmButtonColor  : "#0C0",
        confirmButtonText   : "Ya!",
        cancelButtonText    : "Batal",
        closeOnConfirm      : true
    },function(){

        var elem = $(this);

        $.ajax({
            type    : "POST",
            url     : "<?php echo site_url('con_atk/buat_memo_direct'); ?>",                        
            data    : data,                     
            success : function(data){   
                elem.attr('disabled','disabled');
                elem.closest('tr').find('label').text('SENT');

                alert("SUCCESS");
            }
        });             
    });     
});

note: in the AJAX success, the alert is executed but not the elem

connexo

Cause of the problem

Functions declared the way you did have their own this, so using $(this) no longer gets you the element that triggered the event.

Solution 1

Define var elem = $(this); outside the inner function() (the one containing the ajax call).

Solution 2

bind this at the end of the function() { ... }.bind(this).

Solution 3 (ES6)

Arrow functions do not have their own this so replacing function() by ()=> also does the job, but will only work if you run it in an ES6 capable browser (or if you're using a transpiler like Babel).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ajax - How to use a returned array in a success function

From Dev

How to pass and use a success/failure response with AJAX

From Dev

How can I use an if statement in a $.jQuery ajax post's success:?

From Dev

How to use variable data after Ajax Call Success - Laravel

From Dev

How to pass $(this) in success callback of $.ajax

From Dev

How to Return ajax result in success?

From Dev

How to pass url to ajax success?

From Dev

AJAX - return JS Array and use it in success event

From Dev

How can i use Ajax success data in same template or somewhere else?

From Dev

How to repeat ajax call until success

From Dev

How to return data from ajax success function?

From Dev

how to reload a page using ajax success function

From Dev

how to extract string list values in ajax success

From Dev

How to navigate between divs On Success in ajax call

From Dev

how to show hidden div on ajax success call

From Dev

how to access object method inside ajax success

From Dev

How to replace success:function() with .done() in AJAX

From Dev

how to add a flash message in the ajax success function?

From Dev

How to alert a php array in ajax success function

From Dev

How to repeat ajax call until success

From Dev

How to display the image from ajax success function?

From Dev

How does $.ajax success scope work?

From Dev

how to clear kendo spreadsheet on ajax success event?

From Dev

how to scroll down a div when ajax is success?

From Dev

How to select the result from data success of ajax?

From Dev

How to split the success output from an Ajax post?

From Dev

How to add class active after AJAX success

From Dev

XMLHttpRequest: need to use success and error functions from ajax

From Dev

XMLHttpRequest: need to use success and error functions from ajax

Related Related

  1. 1

    Ajax - How to use a returned array in a success function

  2. 2

    How to pass and use a success/failure response with AJAX

  3. 3

    How can I use an if statement in a $.jQuery ajax post's success:?

  4. 4

    How to use variable data after Ajax Call Success - Laravel

  5. 5

    How to pass $(this) in success callback of $.ajax

  6. 6

    How to Return ajax result in success?

  7. 7

    How to pass url to ajax success?

  8. 8

    AJAX - return JS Array and use it in success event

  9. 9

    How can i use Ajax success data in same template or somewhere else?

  10. 10

    How to repeat ajax call until success

  11. 11

    How to return data from ajax success function?

  12. 12

    how to reload a page using ajax success function

  13. 13

    how to extract string list values in ajax success

  14. 14

    How to navigate between divs On Success in ajax call

  15. 15

    how to show hidden div on ajax success call

  16. 16

    how to access object method inside ajax success

  17. 17

    How to replace success:function() with .done() in AJAX

  18. 18

    how to add a flash message in the ajax success function?

  19. 19

    How to alert a php array in ajax success function

  20. 20

    How to repeat ajax call until success

  21. 21

    How to display the image from ajax success function?

  22. 22

    How does $.ajax success scope work?

  23. 23

    how to clear kendo spreadsheet on ajax success event?

  24. 24

    how to scroll down a div when ajax is success?

  25. 25

    How to select the result from data success of ajax?

  26. 26

    How to split the success output from an Ajax post?

  27. 27

    How to add class active after AJAX success

  28. 28

    XMLHttpRequest: need to use success and error functions from ajax

  29. 29

    XMLHttpRequest: need to use success and error functions from ajax

HotTag

Archive