I have a javascript function that is called when a button is pressed. This function calls another function with an ajax call. If/when this ajax is completed successfully, I would like the pressed button's class to change.
$(".followUser").click(function(){
...
create_friendship(that.userId, that.friendId);
...
}
function create_friendship(user_id, friend_id){
$.ajax({
type: "POST",
...
success: function(data, textStatus, jqXHR){
variableForButtonHere.html("Request sent").removeClass("btn-default").addClass('btn-info');
So far, replacing variableForButtonHere with $(this) has not worked. I have put
var mydata = $(this).data();
window.alert(mydata.userId);
in both functions and in the first function it prints and in the second if prints undefined
I am assuming that $(this) must somehow be passed into the second function. How do I do this?
$(this)other than by parameter. There's no way of passingthiseither, other than by parameter.thisdepends on how a function is called. So, you can set the value ofthisdepending on how you call your function. An example could be howjQuery.proxyfunction utilises it to change the value ofthis.