I've had the following code working for some time in my Rails app:
 $(function () {
     // Change the link's icon while the request is performing
     $(document).on('click', 'a[data-remote]', function () {
         var icon = $(this).find('i');
         icon.data('old-class', icon.attr('class'));
         icon.attr('class', 'glyphicon glyphicon-refresh spin');
     });
    // Change the link's icon back after it's finished.
    $(document).on('ajax:complete', function (e) {
        var icon = $(e.target).find('i');
        if (icon.data('old-class')) {
            icon.attr('class', icon.data('old-class'));
            icon.data('old-class', null);
        }
    })
}
The code replaces a glyphicon with a "refresh" glyphicon and spins it until the AJAX request is complete, then replaces it with the original glyphicon.
I recently noticed the code had stopped working. The AJAX request works fine, but I no longer get my spinning refresh glyphicon during the AJAX call. It's possible an upgrade to Rails 5.1 has caused the problem.
I do have gem 'jquery-rails' in my Gemfile, so I had thought the Rails 5.1 upgrade was fully backward-compatible. It could be some other change that has cause this to break.
I've updated the first function as follows:
$('a[data-remote]').on('click', function () {
    var icon = $(this).find('i');
    icon.data('old-class', icon.attr('class'));
    icon.attr('class', 'glyphicon glyphicon-refresh spin');
});
This works the first time a button is clicked, but if the same button is clicked again, I get no spinner.
Is there a solution to this problem, preferably using the standard DOM API? I messed around with:
document.querySelectorAll('[data-remote]').onclick = function () { ... }
but haven't had any luck.
Am I even close to a workable answer? Would this be a good candidate for using addEventListener?
 
     
    