2

The functionality I am seeking is as follows:

  1. User clicks button that sits inside a DataTable
  2. The button disables and its text changes from 'Manage' to an animated icon from the Font Awesome range of images.
  3. Ajax request is sent and on success a modal pops up with a new dialogue.
  4. The button reverts to it's original form.

Steps 1 and 3 are working fine, it is 2 and 4 that is giving me some issues. I recognise I need to use delegated event handlers to modify the specific button in the DataTable, at the moment there is no change on the page. It should be noted that the site uses Bootstrap.

Here is the column from the DataTable with my button:

 "columns": [
        ...
        {
            "width": "8%",
            "searchable": false,
            "className": "customStyling",
            "render": function () {
                return '<button style="width: 75px" class="btn btn-primary">Manage</button>';
            }
        }
    ]

My onclick event:

$('#myTable tbody').on('click', 'button', function (e) {
    // disable button & change text
    $(this).prop("disabled",true);
    $(this).text('<i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i>');

    // get data from the relevant row
    var row = myTable.row($(this).closest('tr'));
    var data = row.data();

    ... AJAX REQUEST ...
    ... MODAL APPEARS ...

    // enable button, change text back
    $(this).prop("disabled", false);
    $(this).text('Manage');
});

Any assistance is much appreciated.

2
  • 1
    as a remark: replace $(this).text('<i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i>')t to $(this).html('<i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i>')l in your 2 step Commented Sep 6, 2017 at 4:11
  • thank you @diavolic for highlighting this mistake Commented Sep 6, 2017 at 5:11

1 Answer 1

1

"this" is not available inside success callback so $(this).prop("disabled", false); will not work, "this" must be saved to a variable before sending ajax, check the snippet (its a test ajax api, response is fast)

$('button').click(function(){
//save "this" to a variable button
var button = this;
$(button).html('<i class="fa fa-circle-o-notch fa-spin"></i>');
$.ajax({
url:'https://jsonplaceholder.typicode.com/photos',
success:function(data){
   $(button).html('Manage');
}
});

});
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<button>Manage</button>

Sign up to request clarification or add additional context in comments.

4 Comments

I see I didn't make that clear. In the full code, those last two lines sit inside the success and the error callbacks, I just removed the AJAX for now to reduce unnecessary code for this problem.
$(this).text escapes html characters, plz use $(this).html instead, @Alive to Die mentioned this, i think his answer is your solution
Understood the problem, it is because you are using "this" inside the success callback, check my edited answer
this is my issue, turns out I should of included the AJAX code @Ananthakrishnan Baji

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.