This is something I've needed to learn for a while and I'm wondering if any javascript experts can point me in the right direction.
Many libraries I've used seem to accept function pointers as arguments, such as a hook for success, fail, etc. Here is a simple example of this in an application I'm working on:
$('.clock-btn').each(function(){
$(this).click(function(){
$.ajax(
{
url:'punch.php?task_id=' + $(this).data('task-id'),
success: function() {
reverseCSS($(this));
},
error: function() {
window.alert("Clocking time to this project is not allowed.");
}
}
);
});
});
Where the reverseCSS()
function is a function that will change the button to an animation and exchange the words with 'clock-in/clock-out' depending on what it was previously.
However, I cannot gain access to the $(this) variable in the $.ajax
call. This is expected behavior to me, as I know in this example, the $(this)
will be the $.ajax object
, not the item I need from the html.
So, what would be the best method here to access the $(this)
item?
Thanks so much for your help.