jquery - Not work addClass -
i use datatables (jquery plugin) , rows have many buttons doing somethink work (send server request - edit, delete , etc).
i want after click on 1 of bottons, added class name 'disabled' , after doing work class removed.
but class 'disabled' not added. but, if delete $(this).removeclass('disabled');, button class! why? or local problems?
i have html
<a id='change-color' class='btn btn-danger'>change color</a> and js code
$(document).on('click', '#change-color', function(){ $(this).addclass('disabled'); var _this = this; // hmm.. can without spick?.. $.get('/', function(text){ if($(_this).hasclass('btn-danger')){ $(_this).removeclass('btn-danger').addclass('btn-success'); } else { $(_this).removeclass('btn-success').addclass('btn-danger'); } }); $(this).removeclass('disabled'); }); live on jsfiddle
you're removing class after add it; before asynchronous callback executes. remove class inside callback instead:
$(document).on('click', '#change-color', function(){ $(this).addclass('disabled'); var _this = this; // hmm.. can wihout stafff?.. $.get('/', function(text){ if($(_this).hasclass('btn-danger')){ $(_this).removeclass('btn-danger').addclass('btn-success'); } else { $(_this).removeclass('btn-success').addclass('btn-danger'); } // removes class after ajax request completes $(this).removeclass('disabled'); }); });
Comments
Post a Comment